Skip to content

Instantly share code, notes, and snippets.

@shrekuu
Last active April 25, 2019 02:11
Show Gist options
  • Select an option

  • Save shrekuu/8c44ccdf53a251d0ac6cd468e2519025 to your computer and use it in GitHub Desktop.

Select an option

Save shrekuu/8c44ccdf53a251d0ac6cd468e2519025 to your computer and use it in GitHub Desktop.
跳转外部百度地图高德地图驾车线路规划
/**
* 跳转到地图导航界面
* http://lbsyun.baidu.com/index.php?title=uri/api/android
* https://developers.google.com/maps/documentation/urls/guide#directions-action
* @param longitude
* @param latitude
* @param mapAppName browser-浏览器打开, gaode-高德APP, baidu-百度APP,如果没有安装相应APP则使用浏览器打开。
* @param destination 目的地
*/
export function goToOuterMapApp (longitude = 0, latitude = 0, mapAppName = 'baidu', destination = '目的地') {
if (0 === latitude && 0 === longitude) {
console.warn('坐标不正确', latitude, longitude)
return
}
const appBundleName = 'com.example.app'
const amapUrlAndroid = `androidamap://route?sourceApplication=${appBundleName}&dev=0&t=0&dlon=${longitude}&dlat=${latitude}&dname=${destination}`
const amapUrlios = `iosamap://path?sourceApplication=${appBundleName}&dev=0&t=0&dlon=${longitude}&dlat=${latitude}&dname=${destination}`
// 没有百度在图与高德地图时用网页打开百度地图
// const amapWebUrl = `http://uri.amap.com/navigation?to=${longitude},${latitude},${destination}&mode=car&coordinate=gcj02&src=${appBundleName}`
const baidumapUrl = `baidumap://map/direction?destination=name:${destination}|latlng:${latitude},${longitude}&mode=driving&coord_type=gcj02&src=${appBundleName}`
const baidumapWebUrl = `http://api.map.baidu.com/direction?destination=latlng:${latitude},${longitude}|name=${destination}&mode=driving&coord_type=gcj02&output=html&src=${appBundleName}`
// iOS 平台
if (Platform.OS === 'ios') {
Linking.canOpenURL(baidumapUrl).then(supported => {
if (!supported) {
Linking.canOpenURL(amapUrlios).then(supported => {
if (!supported) {
console.log('Can\'t handle url: ' + baidumapUrl)
return Linking.openURL(baidumapWebUrl).catch(e => console.warn(e))
}
return Linking.openURL(amapUrlios).catch(e => console.warn(e))
}).catch(err => console.error('An error occurred(1)', err))
}
return Linking.openURL(baidumapUrl).catch(e => console.warn(e))
}).catch(err => console.error('An error occurred(2)', err))
return
}
// android 平台
Linking.canOpenURL(baidumapUrl).then(supported => {
if (!supported) {
Linking.canOpenURL(amapUrlAndroid).then(supported => {
if (!supported) {
console.log('Can\'t handle url: ' + baidumapUrl)
return Linking.openURL(baidumapWebUrl).catch(e => console.warn(e))
}
return Linking.openURL(amapUrlAndroid).catch(e => console.warn(e))
}).catch(err => console.error('An error occurred(3)', err))
}
return Linking.openURL(baidumapUrl).catch(e => console.warn(e))
}).catch(err => console.error('An error occurred(4)', err))
}
// web 版的使用谷歌地图
export function goToOuterMapApp (latitude = 0, longitude = 0, destination = '目的地') {
if (0 === latitude && 0 === longitude) {
console.warn('坐标不正确', latitude, longitude)
Toast.fail('坐标不正确')
return
}
const baidumapUrl = `baidumap://map/direction?destination=name:${destination}|latlng:${latitude},${longitude}&mode=driving&coord_type=gcj02&src=com.example.app`
const googlemapWebUrl = `http://maps.google.cn/maps?&daddr=${latitude},${longitude}&travelmode=driving`
Linking.canOpenURL(baidumapUrl).then(supported => {
if (!supported) {
console.log('canOpenURL not supported: ' + baidumapUrl)
return Linking.openURL(googlemapWebUrl).catch(e => console.warn(e))
}
return Linking.openURL(baidumapUrl).catch(e => console.warn(e))
}).catch(err => console.error('canOpenURL not supported error: ', err))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment