Vue中使用百度API获取地理位置

Vue中使用百度API获取地理位置

  • 新建一个文件夹 Map.js
export function MP(ak) {  
  return new Promise(function (resolve, reject) {  
    window.onload = function () {  
      resolve(BMap)  
    }  
    var script = document.createElement("script");  
    script.type = "text/javascript";  
    script.src = "http://api.map.baidu.com/api?v=2.0&ak="+ak+"&callback=init";  
    script.onerror = reject;  
    document.head.appendChild(script);  
  })  
}
  • 在VUE中调用,测试环境是将以下代码放在了mounted()中
      // 在session中获取地址
			if(sessionStorage.getItem("address")!=null){
				this.address=sessionStorage.getItem("address");
				console.log("address",this.address);
			}
			else{
        // 等待dom更新完成后执行回调
				this.$nextTick(()=>{
				  //防止在回调函数中使用this有时会不生效,
					var that=this;
          // ak是百度地图开发者平台申请的密钥,需要个人进行申请,最后在局部注册组件时声明
					MP(that.ak).then(BMap=>{
            // Geolocation 定位当前城市信息
						var geolocation = new BMap.Geolocation();
						// 获取当前位置
						geolocation.getCurrentPosition(function getinfo(position){
						        var city = position.address.city; //获取城市信息
						        var province = position.address.province; //获取省份信息
								that.address=province+city
								console.log(that.address);
								//将信息设置到session中
								sessionStorage.setItem("address",that.address);
						    }, function(e) {
						        alert("获取百度定位位置信息失败");
						    }, {provider: 'baidu'});
					})
				})
			}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue使用百度地图API自动获取地址和经纬度,你需要完成以下步骤: 1. 在百度地图开放平台上注册账号并创建应用,获取API密钥。 2. 在Vue项目引入百度地图APIJS文件,在index.html添加以下代码: ``` <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=your_api_key"></script> ``` 将 `your_api_key` 替换为你在百度地图开放平台上创建应用后获取API密钥。 3. 在Vue组件使用以下代码,创建地图实例: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; }, ``` 其,`map-container` 是放置地图的DOM元素的ID。 4. 在Vue组件使用以下代码,获取用户当前位置的经纬度: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition((position) => { if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) { this.latitude = position.point.lat; this.longitude = position.point.lng; } else { alert('定位失败'); } }); }, ``` 其,`geolocation.getCurrentPosition` 方法用于获取用户当前位置的经纬度。如果获取成功,则将经纬度赋值给 `latitude` 和 `longitude` 变量。如果获取失败,则弹出提示框。 5. 在Vue组件使用以下代码,将经纬度转换成具体的地址: ``` mounted() { var map = new BMap.Map("map-container"); this.map = map; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition((position) => { if (geolocation.getStatus() === BMAP_STATUS_SUCCESS) { this.latitude = position.point.lat; this.longitude = position.point.lng; var point = new BMap.Point(this.longitude, this.latitude); var geoc = new BMap.Geocoder(); geoc.getLocation(point, (rs) => { this.address = rs.address; }); } else { alert('定位失败'); } }); }, ``` 其,`BMap.Point` 方法用于创建一个地理位置点的实例,需要传入经度和纬度。`BMap.Geocoder` 方法用于将经纬度转换成具体的地址。`geoc.getLocation` 方法用于获取地址信息,并将地址信息赋值给 `address` 变量。 这样,你就可以在Vue使用百度地图API自动获取地址和经纬度了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值