百度地图拖动点 获取当前坐标,和位置信息。

copy就能用的,改上你自己的秘钥。
安卓手机获取当前定位是不准确的,ios可以,上一篇博客有说怎样获取当前精准定位感兴趣的小伙伴可以去看下
获取用户当前位置精准定位亲测有效。
在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
        .button-full{
            position: fixed;
            bottom: 0;
            left: 0;
            z-index: 999;
            height: 50px;
            width: 100%;
            line-height: 50px;
            font-size: 16px;
            text-align: center;
            color: #fff;
            background: #333;
            text-decoration: none;
        }
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/getscript?v=2.0&ak=你的秘钥"></script>
    <title>地图展示</title>
</head>
<body>
    <div id="allmap"></div>
    <a id="chosedBtn" class=" button-full">选择这个地址</a>
</body>
<script type="text/javascript">
    //创建默认初始化Map实例
    (function(){
         
        mapObj = {
            com: {
                map : new BMap.Map("allmap"),
                infoWindow :  new BMap.InfoWindow("正在载入..", {offset : new BMap.Size(10, -60)}),
                icon: new BMap.Icon("./address.png", new BMap.Size(60, 60)),
                iconOffsetSize: new BMap.Size(0, -20),
                chosedAddr: ""
            },
             
            init: function(){
                var _this = this;
 
                _this.default();
 
                _this.initLocation().then(function(result){
                    //清除掉默认的markerDefault
                    _this.com.map.clearOverlays()
 
                    var marker = result.mk;
                    var point = result.pt;
 
                    //取得当前位置的名字
                    _this.getAddrAccordingPoint(point).then(function(res){
                        _this.changeInfoWindowContent(res)
                        //打开信息窗体
                        _this.com.map.openInfoWindow(_this.com.infoWindow, point); 
                    });
                    
                    //绑定拖动结束事件,获取经纬度
                    marker.addEventListener("dragend", function(){
                        var curPoint = marker.getPosition();
                        console.log(curPoint)   
                        //取得拖动后当前位置的名字
                        _this.getAddrAccordingPoint(curPoint).then(function(res){
                            _this.changeInfoWindowContent(res)
                            //打开信息窗体
                            _this.com.map.openInfoWindow(_this.com.infoWindow, curPoint); 
                        });
                    });
                    marker.addEventListener("click", function(){ 
                        var curPoint = marker.getPosition();
                        _this.getAddrAccordingPoint(curPoint).then(function(res){
                            _this.changeInfoWindowContent(res)
                            //打开信息窗体
                            _this.com.map.openInfoWindow(_this.com.infoWindow, curPoint); 
                        })
                    });
                })
            },
 
            //默认打开地图的初始状态设置
            default: function(){
                var _this = this;
                var point = new BMap.Point(114.062048, 22.54579);
                // 设置初始化地图,设置中心点坐标和地图级别
                _this.com.map.centerAndZoom(point, 12); 
 
                //设置初次打开时,markerDefault,定位初始完毕会被销毁
                var markerDefault = new BMap.Marker(point);
                _this.com.map.addOverlay(markerDefault);
                markerDefault.setIcon(_this.com.icon);
 
                //设置信息窗体宽度
                _this.com.infoWindow.setWidth(220);
            },
 
            //獲取定位初始化地圖
            initLocation: function (){
                var _this = this;
                return new Promise(function(resolve, reject){
                    new BMap.Geolocation().getCurrentPosition(function(r){
                        var lat = r.latitude;
                        var long = r.longitude;
                        var point = new BMap.Point(long, lat)
 
                        var marker = new BMap.Marker(point);
                        marker.setOffset(_this.com.iconOffsetSize)
                        marker.setIcon(_this.com.icon);
                        marker.setAnimation(BMAP_ANIMATION_BOUNCE); // 图标跳动, 只是用pc端,移动端失效
                        var initObj = {
                            pt: point,
                            mk: marker
                        }
 
                        // 将标注添加到地图中
                        _this.com.map.addOverlay(marker);
                         
 
                        //允许拖动           
                        marker.enableDragging();
                        //不允许被clearOverlays方法清除
                        marker.disableMassClear();
 
                        //地图指向当前的点(平移动画)
                        _this.com.map.panTo(point);
                        // 初始化地图,设置中心点坐标和地图级别
                        _this.com.map.centerAndZoom(point, 16); 
 
                        resolve(initObj);
                    })
                })
            },
 
            //根据经纬度解析出位置的名称
            getAddrAccordingPoint: function(point){
                var _this = this;
                return new Promise(function(resolve, reject){
                    new BMap.Geocoder().getLocation(point, function(result){     
                        if (result){     
                            if(!!result.surroundingPois[0]){
                                _this.com.chosedAddr = result.address + " " +result.surroundingPois[0].title; 
                            }else{
                                _this.com.chosedAddr = result.address; 
                            }
                            resolve(_this.com.chosedAddr)
                        }     
                    });
                })
            },
 
            //添加标注地址信息
            changeInfoWindowContent: function(content){
                var _this = this;
                _this.com.infoWindow.setContent(content);
            }
        };
 
        //开始地图
        mapObj.init();
 
        // document.getElementById('chosedBtn').addEventListener('click', function(){
        //     location.href = '<{:U("Channel/addChannelFollowLog")}>?address='+mapObj.com.chosedAddr
        // },false)   
         
    })()
</script>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值