vue+高德地图api (总结)

引入地图:public 中index.html引入

//&后面为引入的工具
 <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key="你的key"&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script> 

新创地图
data中定义map:’’
实列地图:

<div  id="container" style="width: 500px; height: 300px>
       <!-- 搜索框 -->
     <el-input v-model="searchMap" id="tipinput"></el-input>
</div>
 this.map = new AMap.Map("container", {
                resizeEnable: true,
            });

api:

//地图   定位
        autoLocation() {
            var options = {
                showButton: true, //是否显示定位按钮
                buttonPosition: "LB", //定位按钮的位置
                /* LT LB RT RB */
                buttonOffset: new AMap.Pixel(10, 20), //定位按钮距离对应角落的距离
                showMarker: true, //是否显示定位点
                markerOptions: {
                    //自定义定位点样式,同Marker的Options
                    offset: new AMap.Pixel(-18, -36),
                    content:
                        '<img src="https://a.amap.com/jsapi_demos/static/resource/img/user.png" style="width:36px;height:36px"/>',
                },
                showCircle: true, //是否显示定位精度圈
                circleOptions: {
                    //定位精度圈的样式
                    strokeColor: "#0093FF",
                    noSelect: true,
                    strokeOpacity: 0.5,
                    strokeWeight: 1,
                    fillColor: "#02B0FF",
                    fillOpacity: 0.25,
                },
            };
     AMap.plugin(["AMap.Geolocation"], () => {
                var geolocation = new AMap.Geolocation(options);
                this.map.addControl(geolocation);
                geolocation.getCurrentPosition();
            });

地图搜索

 var autoOptions = {
                input: "tipinput", //html中input id
            };
            var auto = new AMap.Autocomplete(autoOptions);
            var placeSearch = new AMap.PlaceSearch({
                map: this.map, // 你的地图示例
            });
            AMap.event.addListener(auto, "select", select); //注册监听,当选中某条记录时会触发
            function select(e) {
                placeSearch.setCity(e.poi.adcode);
                placeSearch.search(e.poi.name); //关键字查询查询
            }

获取经纬度,经纬度对应地址信息

//经纬度
            let showInfoClick = (e) => {
                  //经纬度
                let lng = e.lnglat.getLng();
                let lat = e.lnglat.getLat();
                console.log(e.lnglat.getLng(), e.lnglat.getLat());

                var lnglatXY = [lng, lat]; // 地图上所标点的坐标
                let _this = this;
                AMap.service("AMap.Geocoder", function () {
                    // 回调函数
                    let geocoder = new AMap.Geocoder({});
                    geocoder.getAddress(lnglatXY, (status, result) =>{
  					//result.regeocode.formattedAddress为地址
                            );
                        }
                       
                    });
                });
            };
            //添加地图点击事件
            this.map.on("click", showInfoClick);

经纬度转换

//高德坐标转百度(传入经度、纬度)
        bd_encrypt() {
            var X_PI = (Math.PI * 3000.0) / 180.0;
            var x = this.storeInfo.lng,
                y = this.storeInfo.lat;
            var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI);
            var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI);
            this.storeInfo.lng = z * Math.cos(theta) + 0.0065;
            this.storeInfo.lat = z * Math.sin(theta) + 0.006;
        },
        //百度坐标转高德(传入经度、纬度)
        bd_decrypt() {
            var X_PI = (Math.PI * 3000.0) / 180.0;
            var x = this.storeInfo.lng - 0.0065;
            var y = this.storeInfo.lat - 0.006;
            var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
            var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
            this.storeInfo.lng = z * Math.cos(theta);
            this.storeInfo.lat = z * Math.sin(theta);
        },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值