springboot8==调用百度地图API从浏览器获取经纬度,后端使用geodesy依赖计算配送距离

见百度地图API调用文档

jspopularGL | 百度地图API SDK

 

 ================

<!--引入百度地图API用于从浏览器获取当前经纬度-->
<script type="text/javascript"
        src="http://api.map.baidu.com/api?v=2.0&ak=CoeKSNi3O0VoiopZZTwGA3HFT9AjcWGX"></script>

===============

<span><img src="./images/time.png"/> 距离{{userLocation}}km</span>
<span><img src="./images/money.png"/> 配送费{{sendMoney}}元</span>
<span><img src="./images/location.png"/> 预计时长{{sendTime}}min</span>

=========

//默认值
userLocation: 1,
sendMoney: 1,
sendTime: 1,

=======

getLocation() {
    let _this = this
    var map = new BMap.Map("allmap");
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(async function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mk = new BMap.Marker(r.point);
            map.addOverlay(mk);
            map.panTo(r.point);
            alert('您的位置,是否允许使用:' + r.point.lng + ',' + r.point.lat);

            const location = {
                //往后端传输经纬度
                latitude: r.point.lng,
                longitude: r.point.lat
            }
            const res = await getLocation2(location)
            // alert(res.code)
            if (res.code === 1) {
                _this.userLocation = res.data
                _this.sendMoney = _this.userLocation * 1
                _this.sendTime = _this.userLocation * 2
                console.log(_this.userLocation + '千米')
            }
        } else {
            alert('failed' + this.getStatus());
        }
    });
},

===========

//获取当前距离
function getLocation2(location) {
    return $axios({
        'url': '/dish/getLocation2',
        'method': 'get',
        params: location
    })
}

==========

/**
 * 根据前端传回的经纬度,计算获取距离
 */
@GetMapping("/getLocation2")
public R getLocation(String latitude, String longitude) {
    log.info("经纬度" + latitude + "经纬度" + longitude);
    double a = Double.parseDouble(latitude);
    //用户当前坐标
    GlobalCoordinates userSource = new GlobalCoordinates(Double.parseDouble(latitude), Double.parseDouble(longitude));
    //店铺位置坐标   经纬度104.07274727经纬度30.57899372
    GlobalCoordinates businessTarget = new GlobalCoordinates(104.07274728, 30.57899372);
    double meterDouble = getDistanceMeter(userSource, businessTarget, Ellipsoid.Sphere);
    int meter = (int) meterDouble;
    log.info("距离多少千米" + meter / 1000);
    return R.success(meter / 1000);
}

===========================================

。。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值