手动打点 点击地图打点 获取位置信息

本文详细介绍了如何在网页中使用Amap组件实现地图点击打点,通过JavaScript获取用户位置并进行标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手动打点 点击地图打点 获取位置信息

html

	 <point-map ref="pointMap" style="width: 62.8% ;height: 350px"/>  // 组件方式引入 import  (前文有讲)

页面初始化完成后增加方法 (地图加载完成)
js

	let that = this; // 手动打点
	setTimeout(() => {
	this.$refs.pointMap.amapManager.on("click", function (e) {
	console.log(e) // 位置信息
	that.$refs.pointMap.seachPoint(e.lnglat.lng, e.lnglat.lat); //打点
	})
	}, 1000)

此时点击地图就会获取到位置信息

效果:
在这里插入图片描述

首先需要在Vue项目中引入百度地图JavaScript API。 1. 在`index.html`文件中添加百度地图JavaScript API的引用: ```html <!-- 引入百度地图 JavaScript API --> <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=您的AK"></script> ``` 其中,`AK`是你在百度地图开发者中心中申请的密钥。 2. 在Vue组件中添加地图容器和定位按钮: ```html <template> <div> <!-- 地图容器 --> <div id="map"></div> <!-- 定位按钮 --> <button @click="getLocation">定位</button> </div> </template> ``` 3. 在Vue组件的`<script>`标签中编写JavaScript代码: ```js export default { data() { return { // 地图对象 map: null, // 定位标记 marker: null, // 定位信息 location: null }; }, methods: { // 初始化地图 initMap() { const map = new BMap.Map('map'); map.centerAndZoom(new BMap.Point(116.404, 39.915), 18); this.map = map; }, // 获取当前位置信息 getLocation() { const geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition( position => { this.location = position; this.showLocation(); }, { enableHighAccuracy: true } ); }, // 在地图上显示位置信息 showLocation() { const { point, address } = this.location; if (!this.marker) { const marker = new BMap.Marker(point); this.map.addOverlay(marker); this.marker = marker; } else { this.marker.setPosition(point); } this.map.panTo(point); alert(`当前位置:${address}`); } }, mounted() { this.initMap(); } }; ``` 在上述代码中,我们首先定义了`map`、`marker`和`location`三个变量,分别表示地图对象、定位标记和位置信息。在`initMap`方法中,我们创建了一个地图对象并将其初始化。在`getLocation`方法中,我们通过`BMap.Geolocation`获取当前位置信息,并将其保存在`location`变量中。然后我们调用`showLocation`方法,在地图上显示定位标记,并弹窗显示当前位置信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值