Vue结合百度地图Api 逆地理编码

通过点击地图上位置点 获取相应的经纬度及省市区街道
HTML代码

<el-dialog :visible.sync="mapShow" style="padding:0px 0px 0px 0px; width: 1100px;margin-left: 400px"  :close-on-click-modal='false'>
                <div id="info"></div>
                <div id="info2"></div>
                <baidu-map :center=center :zoom=zoom :scroll-wheel-zoom="true" style="width:520px;height:340px;border:1px solid gray" @ready="handler" @click="getClickInfo" >
                    <!-- 必须给容器指高度,不然地图将显示在一个高度为0的容器中,看不到 -->
                    <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
                    <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true"></bm-geolocation>
                    <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
                </baidu-map>
                <!-- 因为我采用的是全局注册,所以不用再在该页面上注册components -->

                <el-button type="primary" @click="mapFuncation()">确认</el-button>
                <el-button @click="mapShow = false">取消</el-button>
            </el-dialog>

数据

				mapShow:false,
                center:"",
				zoom:"",
                j:0,
                w:0,
				mapProvince:"",
                mapCity:"",
                mapDistrict:"",
                mapStreet:"",
                cityUrl:"https://api.map.baidu.com/reverse_geocoding/v3/?ak=“你的AK”&output=json&coordtype=wgs84ll&location=",

方法

// 地图经纬度开始------------------------------------------------------
            showTrue:function(){
                // alert(1);
                this.center = "拱墅区";
                this.zoom = 13;
                this.mapShow=true;
            },
            getClickInfo(e) {
                window.console.log(JSON.stringify(e.point))
                this.j = e.point.lng;
                this.w = e.point.lat;
                document.getElementById("info").innerHTML = "经度:" + e.point.lng + "   纬度:" + e.point.lat + "</br>";
                var surl = this.cityUrl+e.point.lat+","+e.point.lng;
                Vue.jsonp(surl).then(json=>{
                    this.mapProvince = json.result.addressComponent.province;
                    this.mapCity = json.result.addressComponent.city;
                    this.mapDistrict = json.result.addressComponent.district;
                    this.mapStreet  = json.result.addressComponent.street;
                    document.getElementById("info2").innerHTML = "省:" + this.mapProvince + "    市:" + this.mapCity + "</br>"+"区:" + this.mapDistrict+ "    "+"街道:"+this.mapStreet ;
                }).catch(err => {
                    var data=err;
                    var jsonData = JSON. stringify(data);
                    alert("获取位置失败"+jsonData);
                })
            },mapFuncation(){
                this.formData.longitude = this.j;
                this.formData.latitude = this.w;
                this.formData.province = this.mapProvince;
                this.formData.city = this.mapCity;
                this.formData.area = this.mapDistrict;
                this.formData.county = this.mapStreet;
                this.mapShow=false;
            },
           
            // 地图经纬度结束---------------------------------------------------------

在这里插入图片描述
正常访问百度地图API获取到的值

通过jsonp将跳转链接获取到的值返回成json解析

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 高德地图地理编码可以使用 AMap JavaScript API 实现。以下是一个简单的示例: 首先需要在 `index.html` 中引入高德地图 JavaScript API: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=your-amap-key"></script> ``` 然后在 Vue 组件中,可以使用以下代码获取当前位置的地理编码信息: ```vue <template> <div> <div ref="map" style="width: 100%; height: 400px;"></div> <div>{{ address }}</div> </div> </template> <script> export default { data() { return { address: "" }; }, mounted() { // 创建地图 const map = new AMap.Map(this.$refs.map, { zoom: 16 }); // 获取当前位置 map.plugin("AMap.Geolocation", () => { const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为 false timeout: 10000, // 超过 10 秒后停止定位,默认值为 无穷大 maximumAge: 0, // 定位结果缓存时间,默认值为 0 convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认为 true showButton: true, // 显示定位按钮,默认为 true buttonPosition: "LB", // 定位按钮的位置,默认为 'LB',左下角 buttonOffset: new AMap.Pixel(10, 10), // 定位按钮距离容器左下角的偏移量,默认为 Pixel(10, 20) showMarker: true, // 定位成功后在定位到的位置显示点标记,默认为 true showCircle: true, // 定位成功后用圆圈表示定位精度范围,默认为 true panToLocation: true, // 定位成功后将定位到的位置作为地图中心点,默认为 true zoomToAccuracy: true // 定位成功后自动调整地图缩放级别以适合定位结果的范围,默认为 true }); geolocation.getCurrentPosition((status, result) => { if (status === "complete") { this.getAddress(result.address); } else { console.log("获取当前位置信息失败"); } }); }); }, methods: { // 地理编码 getAddress(address) { this.address = address; } } }; </script> ``` 在 `mounted` 钩子函数中,我们使用 `AMap.Geolocation` 插件获取当前位置信息。然后在 `getCurrentPosition` 回调函数中,我们通过 `result.address` 获取当前位置的地理编码信息,并将其赋值给 `this.address`,以在页面中显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值