美文网首页
百度地图定位后算距离

百度地图定位后算距离

作者: 飞飞廉 | 来源:发表于2019-09-29 14:51 被阅读0次
    init() {
                // 百度地图API功能
                const that = this;
                var map = new BMap.Map("allmap");
                map.centerAndZoom(new BMap.Point(116.404, 39.915), 12);
                var geolocation = new BMap.Geolocation();
                // 开启SDK辅助定位
                geolocation.enableSDKLocation();
                geolocation.getCurrentPosition(function(r){
                    if(this.getStatus() == 0){
                        var driving = new BMap.DrivingRoute(map, options);
                        var start = new BMap.Point(r.point.lng, r.point.lat);
                        var end = new BMap.Point(that.tplLongitude, that.tplLatitude);
                        // 计算定位地点和门店的距离
                        var searchComplete = function (results){
                            if (transit.getStatus() != 0){
                                return;
                            }
                            var plan = results.getPlan(0);
                            const distance = plan.getDistance(true);
                            that.distance = distance;
                        }
                        var transit = new BMap.DrivingRoute(map, {
                            onSearchComplete: searchComplete
                        });
                        transit.search(start, end);
                    }
                    else {
                        console.log('failed'+this.getStatus());
                    }
                });
            }
    

    测试的时候注意用真实的经纬度坐标,不然会一直报错~

    相关文章

      网友评论

          本文标题:百度地图定位后算距离

          本文链接:https://www.haomeiwen.com/subject/rbvfpctx.html