亲测可行...
react-native 将目标地址的经纬度传给高德或者百度来实现从当前位置到目标地址的路线规划。
IOS端
配置白名单 由于iOS的限制,iOS系统在9之后的版本中,如果开发者的app希望调起高德地图,必须在自己app的设置中配置白名单。
配置方法:
1、找到您的Info.plist文件
2、在文件中添加key:LSApplicationQueriesSchemes,类型是Array,如果曾经添加过,无需再次添加。
3、Array中添加一个item,类型为String,值为iosamap。
添加完成后截图如下:
工具类MyUtil.js
import { Linking, Platform } from 'react-native';
var MyUtil = {
/**
* 跳转到导航界面
* @param lon
* @param lat
* @param name
* @param targetAppName browser-浏览器打开, gaode-高德APP, baidu-百度APP,如果没有安装相应APP则使用浏览器打开。
*/
turn2MapApp(lon = 0, lat = 0, targetAppName = 'baidu', name = '目标地址'){
if (0 == lat && 0 == lon) {
console.warn('暂时不能导航');
return;
}
let url = '';
let webUrl = `http://uri.amap.com/navigation?to=${lon},${lat},${name}&mode=bus&coordinate=gaode`;
let webUrlGaode = `http://uri.amap.com/navigation?to=${lon},${lat},${name}&mode=bus&coordinate=gaode`;
let webUrlBaidu = `http://api.map.baidu.com/direction?destination=latlng:${lat},${lon}|name=${name}&mode=transit&coord_type=gcj02&output=html&src=mybaoxiu|wxy`;
url = webUrl;
if (Platform.OS == 'android') {//android
if (targetAppName == 'gaode') {
// webUrl = 'androidamap://navi?sourceApplication=appname&poiname=fangheng&lat=36.547901&lon=104.258354&dev=1&style=2';
url = `androidamap://route?sourceApplication=appname&dev=0&m=0&t=1&dlon=${lon}&dlat=${lat}&dname=${name}`;
webUrl = webUrlGaode;
} else if (targetAppName == 'baidu') {
url = `baidumap://map/direction?destination=name:${name}|latlng:${lat},${lon}&mode=transit&coord_type=gcj02&src=thirdapp.navi.mybaoxiu.wxy#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end`;
webUrl = webUrlBaidu;
}
} else if (Platform.OS == 'ios') {//ios
if (targetAppName == 'gaode') {
url = `iosamap://path?sourceApplication=appname&dev=0&m=0&t=1&dlon=${lon}&dlat=${lat}&dname=${name}`;
webUrl = webUrlGaode;
} else if (targetAppName == 'baidu') {
url = `baidumap://map/direction?destination=name:${name}|latlng:${lat},${lon}&mode=transit&coord_type=gcj02&src=thirdapp.navi.mybaoxiu.wxy#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end`;
webUrl = webUrlBaidu;
}
}
Linking.canOpenURL(url).then(supported => {
if (!supported) {
console.log('Can\'t handle url: ' + url);
return Linking.openURL(webUrl).catch(e => console.warn(e));
} else {
return Linking.openURL(url).catch(e => console.warn(e));
}
}).catch(err => console.error('An error occurred', err));
},
};
module.exports = MyUtil;
调用:
MyUtil.turn2MapApp(longitude, latitude, 'gaode');
参考链接:
高德
http://lbs.amap.com/api/amap-mobile/gettingstarted/
http://lbs.amap.com/api/uri-api/guide/travel/route
http://lbs.amap.com/api/amap-mobile/guide/android/route
http://lbs.amap.com/api/amap-mobile/guide/ios/route
百度
http://lbsyun.baidu.com/index.php?title=uri/api/web