百度地图定位后算距离

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());
                }
            });
        }

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

你可能感兴趣的:(百度地图定位后算距离)