uniapp使用Vue Baidu Map(百度地图),页面加载自动获取经纬度

参考

申请百度地图的key

百度地图 JavaScript API v2.0类参考

Vue Baidu Map 定位控件

VUE BAIDU MAP

地图上标记点

百度地图的zoom级别介绍

百度地图Geolocation的getStatus状态值

uniapp的map组件

项目安装vue-baidu-map

初始化npm工程

若项目之前未使用npm管理依赖(项目根目录下无package.json文件),先在项目根目录执行命令初始化npm工程:

npm init -y

cli项目默认已经有package.json了。HBuilderX创建的项目默认没有,需要通过初始化命令来创建。

安装依赖

在项目根目录执行命令安装npm包:

npm install vue-baidu-map --save

使用

安装完即可使用npm包,js中引入npm包:

import package from 'packageName'
const package = require('packageName')

uni-app支持使用npm安装第三方包

https://uniapp.dcloud.io/frame?id=npm%E6%94%AF%E6%8C%81

在项目的main.js中加入

由于我的项目中基本都要用到地图,所以我全局引入的(项目根目录>main.js)

// 百度地图
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: 'hMCSwlS4c3QxxxxxFmgdSST5G8T40YX'
})

这里要用你自己的key,自己去申请一个

页面中使用

 

html

<view class="map">
<baidu-map class="map-contain" :scroll-wheel-zoom="true" :center="center" :zoom="zoom" MapType="BMAP_SATELLITE_MAP" @ready="mapReady">
	<bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" @locationSuccess="getMyLocation()" :showAddressBar="true"
	 :autoLocation="true"></bm-geolocation>
	 <bm-marker @dragend="markerDrag" :position="center" :dragging="true" animation="BMAP_ANIMATION_BOUNCE">
	 <!--<bm-label content="我爱北京天安门" :labelStyle="{color: 'red', fontSize : '24px'}" :offset="{width: -35, height: 30}"/> -->
	 </bm-marker>
</baidu-map>
</view>

js

zoom:18,//地图相关设置
center:{lng:0,lat:0}
markerDrag(x){
				console.log( x.point);
				this.center.lat = x.point.lat;
				this.center.lng = x.point.lng;
			},
			mapReady({BMap, map}) {
				// 下面注释是百度地图API官方实现方法
				// console.log(1)
				// console.log(map);
				// console.log(BMap);
				let that = this;
				var geolocation = new BMap.Geolocation();
				// 开启SDK辅助定位
				geolocation.enableSDKLocation();
				geolocation.getCurrentPosition(function(r) {
					console.log("d");
					console.log(r);
					//getStatus拿到的是状态信息,失败与否
					if (this.getStatus() == BMAP_STATUS_SUCCESS) {
						// var mk = new BMap.Marker(r.point);
						// map.addOverlay(mk);//将覆盖物添加到地图中
						// map.panTo(r.point);//将地图的中心点更改为给定的点
						that.center.lng = r.point.lng;
						that.center.lat = r.point.lat;
						that.showToast('您所在位置为经度:' + r.point.lng + ',纬度:' + r.point.lat);
						// alert('您的位置:' + r.point.lng + ',' + r.point.lat);
					} else {
						that.showToast("位置信息获取失败,"+ this.getStatus());
					}
				}, {
					enableHighAccuracy: true//要求浏览器获取最佳结果
				})
			},

其他一些文章

推荐:百度地图JavaScript API获取用户当前经纬度和详细地理位置,反之通过详细地理位置获取当前经纬度

vue-baidu-map 的简单使用
vue-baidu-map进入页面自动定位的解决方案
uni-app 之地图 map
uni-app 支持多端第三方地图定位的方法
vue结合百度地图(vue-baidu-map)根据经纬度查询省市街道信息

以上是转载

https://blog.csdn.net/gabriel_wei/article/details/105849565


完整代码

main.js

import Vue from 'vue'
import App from './App'
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap,{
	  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
	  ak: 'v3Cg0hRvbX7cuUSKG2sEklyeP8iw5eye'
})

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()

地图页完整代码

<template>

	<view class="map">
		<baidu-map class="map-contain" :scroll-wheel-zoom="true" :center="center" :zoom="zoom" MapType="BMAP_SATELLITE_MAP"
		 @ready="mapReady">
			<bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" @locationSuccess="getMyLocation()" :showAddressBar="true"
			 :autoLocation="true"></bm-geolocation>
			<bm-marker @dragend="markerDrag" :position="center" :dragging="true" animation="BMAP_ANIMATION_BOUNCE">
				<!--<bm-label content="我爱北京天安门" :labelStyle="{color: 'red', fontSize : '24px'}" :offset="{width: -35, height: 30}"/> -->
			</bm-marker>
		</baidu-map>
	</view>

</template>
<script>
	export default {
		onLoad(res) {
			console.log("加载") 
			// console.log("加载百度地图")
			// var util = require('http://api.map.baidu.com/getscript?v=2.0&ak=v3Cg0hRvbX7cuUSKG2sEklyeP8iw5eye&services=&t=20200824135534');  //require这个js模块  
			// console.log(util)

		},
		data() {
			return {
				zoom: 18, //地图相关设置
				center: {
					lng: 0,
					lat: 0
				}
			}
		},
		methods: {
			markerDrag(x) {
				console.log(x.point);
				this.center.lat = x.point.lat;
				this.center.lng = x.point.lng;
			},
			mapReady({
				BMap,
				map
			}) {
				// 下面注释是百度地图API官方实现方法
				// console.log(1)
				// console.log(map);
				// console.log(BMap);
				let that = this;
				var geolocation = new BMap.Geolocation();
				// 开启SDK辅助定位
				geolocation.enableSDKLocation();
				geolocation.getCurrentPosition(function(r) {
					console.log("d");
					console.log(r);
					//getStatus拿到的是状态信息,失败与否
					if (this.getStatus() == BMAP_STATUS_SUCCESS) {
						// var mk = new BMap.Marker(r.point);
						// map.addOverlay(mk);//将覆盖物添加到地图中
						// map.panTo(r.point);//将地图的中心点更改为给定的点
						that.center.lng = r.point.lng;
						that.center.lat = r.point.lat;
						console.log('您所在位置为经度:' + r.point.lng + ',纬度:' + r.point.lat);
						// alert('您的位置:' + r.point.lng + ',' + r.point.lat);
					} else {
						console.log("位置信息获取失败," + this.getStatus());
					}
				}, {
					enableHighAccuracy: true //要求浏览器获取最佳结果
				})
			},

		},

	}
</script>

<style>
template{
	width: 100%;
	height: 100%;
}
.map{
	width: 100%;
	height: 600px;
}
.map-contain{
	width: 100%;
	height: 100%;
}
</style>

 

 

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
要在Vue使用百度地图API自动获取地址和经纬度,你需要完成以下步骤: 1. 在百度地图开放平台上注册账号并创建应用,获取API密钥。 2. 在Vue项目中引入百度地图API的JS文件,在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自动获取地址和经纬度了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值