react-native 点击跳转到高德、百度地图并显示路线规划

参考来自: https://www.jianshu.com/p/ca8d03b89410

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') {
                console.log('百度地图--传入的经纬度===','lat==',lat,'lon===',lon,'name===',name ,    )

                //  mode = walking表示为步行路线
                //url = `baidumap://map/direction?region=beijing&origin=39.98871,116.43234&destination=40.057406655722,116.2964407172&coord_type=bd09ll&mode=walking&src=andr.baidu.openAPIdemo`

                 url = `baidumap://map/direction?destination=name:${name}|latlng:${lat},${lon}&mode=walking&coord_type=bd09ll&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;

使用
注意: lat , lon 注意传入的经纬度要对应两个地图的坐标系、高德就传入高德坐标系的经纬度、百度就传入百度坐标系的经纬度,
关于坐标系的转换也有总结了:https://www.jianshu.com/p/2bc62086ae4c

import MyUtil from '../common/MyUtil'

//调用
MyUtil.turn2MapApp(lon, lat, 'baidu');

你可能感兴趣的:(react-native 点击跳转到高德、百度地图并显示路线规划)