uniapp 百度地图 位置点击选择

<template>
	<view>
		<view class="allmap-container">
			<view @click="allmap.onClick" :prop="mapList" :change:prop="allmap.updateinitMap" id="allmap"></view>
		</view>
		<view style="position: relative; z-index: 999;">


			<view @click="nextTo" class="bottomPbulic">
				下一步
			</view>
		</view>

	</view>
</template>
<script module="allmap" lang="renderjs">
	import {
		mymap
	} from "static/map.js";
	let map;


	export default {
		data() {
			return {
				map: null,
				ak: '',
				ownerInstance: null,
			}
		},


		methods: {
			updateinitMap(newValue, oldValue, ownerInstance, instance) {
				this.initMap(newValue)
				this.ownerInstance = ownerInstance

			},
			onClick(event, isClick) {
				// 调用 service 层的方法
				this.ownerInstance.callMethod('onViewClick', {
					maplist: event,
					isClick: isClick
				})
			},
			initMap(mapList) {
				this.$nextTick(() => {
					let _this = this;
					mymap(_this.ak).then((mymap) => {
						let list = mapList
						map = new BMapGL.Map("allmap");
						let point = new BMapGL.Point(list.lng, list.lat);
						map.centerAndZoom(point, 16);
						map.setTilt(0);
						map.setHeading(0);
						map.enableScrollWheelZoom();
						var marker = new BMapGL.Marker(point);
						map.addOverlay(marker);
						map.on('click', (e) => {
							console.log(e)
							map.clearOverlays();
							var marker = new BMapGL.Marker(new BMapGL.Point(e.latlng.lng, e.latlng
								.lat));
							map.addOverlay(marker);
							_this.onClick(e.latlng, true)
						})
					});

				});
			},

		}
	}
</script>
<script>
	export default {
		data() {
			return {
				mapList: {
					lat: '',
					lng: ''
				},
				isClick: false,
				latlng: {},
				isEdit: false,
				list: {
					plotType: '',
					plotName: '',
					id:''
				}
			};
		},
		onLoad(options) {

			if (options.isEdit) {
				let list = JSON.parse(options.list)
				delete list.coordinates
				console.log(list)
				this.list = list
				this.isEdit = options.isEdit

			}
			let that = this
			uni.getLocation({
				type: 'wgs84',
				geocode: true, //设置该参数为true可直接获取经纬度及城市信息
				success: function(res) {
					console.log(res)
					that.isMap = true
					that.mapList.lat = res.latitude;
					that.mapList.lng = res.longitude;
				},
				fail: function(err) {
					console.log("获取定位失败", err);
					uni.showToast({
						title: '获取定位失败',
						icon: 'none'
					});
				}
			});
		},

		methods: {
			rightClick() {
				uni.navigateBack({
					delta: 1
				})
			},
			onViewClick(detail) {
				this.latlng = detail.maplist
				this.isClick = detail.isClick
			},
			gcj02tobd09(lng, lat) {
				var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
				var PI = 3.1415926535897932384626;
				var a = 6378245.0;
				var ee = 0.00669342162296594323;
				var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
				var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
				var bd_lng = z * Math.cos(theta) + 0.0065;
				var bd_lat = z * Math.sin(theta) + 0.006;
				return {
					longitude: bd_lng,
					latitude: bd_lat
				}
			},
			nextTo() {
				let coordinates = {
					latitude: '',
					longitude: ''
				}
				if (this.isClick) {
					coordinates.latitude = this.latlng.lat
					coordinates.longitude = this.latlng.lng
				} else {
					coordinates.latitude = this.mapList.lat
					coordinates.longitude = this.mapList.lng
				}
				let b9 = this.gcj02tobd09(coordinates.longitude, coordinates.latitude)
				let list = {
					coordinates: JSON.stringify([coordinates]),
					...this.list
				}

				if (this.isEdit) {
					uni.navigateTo({
						url: `/pages/parcelInformation/parcelInformation?isEdit=${true}&list=${JSON.stringify(list)}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/parcelInformation/parcelInformation?list=${JSON.stringify(list)}`
					})
				}
			},
		}
	};
</script>

<style scoped lang="scss">
	#allmap {
		width: 100vw;
		height: 100vh;
	}

	.bottomBox {
		width: 670rpx;
		height: 100rpx;
		background: #00B561;
		border-radius: 50rpx;
		margin: 12rpx auto;
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 100rpx;
		text-align: center;
	}
</style>

map.js 地图

export function mymap(ak) {
  return new Promise(function(resolve, reject) {
    window.init = function() {
      resolve(mymap)
    }
    var script = document.createElement('script')
    script.type = 'text/javascript'
    script.src = `http://api.map.baidu.com/api?v=1.0&type=webgl&ak=${ak}&callback=init`
    script.onerror = reject
    document.head.appendChild(script)
  })
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾仁炒饭_111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值