uniapp 高德地图定位功能

效果图

在这里插入图片描述

<template>
	<view>
		<view id="container"></view>
		<view class="control">
			<view class="enlarge" @tap="map.zoomIn()">
				<image src="../../static/common/add.png"></image>
			</view>
			<view class="split"></view>
			<view class="narrow" @tap="map.zoomOut()">
				<image src="../../static/common/reduce.png"></image>
			</view>
			<view class="location" @tap="getCenter()">
				<image src="../../static/common/position.png"></image>
			</view>
		</view>
		<!-- 顶部展示 -->
		<view class="controlLeftView">
			<view class="controlLeft">总里程:12079.6km</view>
			<view class="controlLeft">时速:8.3km/h</view>
			<view class="controlLeft">方向:正北</view>
		</view>
		<!-- 底部展示 -->
		<view class="controlLeftBottomView">
			<view class="flex">
				<view v-if="state == 1" class="title" style="
            box-shadow: 0px 2rpx 4rpx 0px rgba(225, 225, 225, 1);
            border: 2rpx solid #1271ff;
            color: #1271ff;
          ">行驶</view>
				<view v-if="state != 1" class="title">停驶</view>
				<view class="time">05-23 12:00:00</view>
				<view class="mi">距我50km</view>
			</view>
			<view class="flex">
				<image src="../../static/dist/iconLocation.png" mode=""></image>
				<view class="city"> 北京市海淀区西三环北路86号院东北方向200</view>
			</view>
		</view>
		<!-- <image src='../../static/car.png'></image> -->
	</view>
</template>

<script>
	import {
		getLocation
	} from "../../utils/http";
	const $time = require("../../utils/time");
	export default {
		data() {
			return {
				car: '../../static/car.png',
				map: null,
				marker: null,
				lineArr: [],
				totalMileage: "", //总里程
				direction: "", //方向
				address: "", //地区
				locationTime: "", //时间
				Kilometers: "", //距离卡米数
				speed: "", //时速
				plateNo: "", //车牌
				state: 1, //行驶状态
				str: {
					// "vin": "LZGCR2T65HX058177",
					// "plateNo": "赣CQ6066",
					vin: "LSSS6PEB6KT008136",
					plateNo: "陕YH0009",
					brand: "陕汽牌",
				},
				lat: null,
				lng: null,
			};
		},
		onLoad() {
			uni.setNavigationBarTitle({
				title: "车辆定位",
			});
			// #ifdef H5
			this.loadScrpit();
			// #endif
			setTimeout(() => {
				this.initMap();
			}, 1000);
		},
		mounted() {
			// this.getLocations();
		},
		beforeDestroy() {
			this.map && this.map.destroy();
		},
		methods: {
			loadScrpit() {
				var script = document.createElement("script");
				script.src =
					"https://webapi.amap.com/maps?v=1.4.15&key=a37513933f694eee481fb36d346cfe98";
				document.body.appendChild(script);
			},
			async getLocations() {
				const res = await getLocation(this.str);
				console.log(res);
				this.lat = res.content.latitude;
				this.lng = res.content.longitude;
				this.totalMileage = res.content.totalMileage;
				this.direction = res.content.direction;
				this.address = res.content.address;
				this.locationTime = $time.formatTime(res.content.locationTime);
				this.speed = res.content.speed;
				this.plateNo = res.content.plateNo;
				this.state = res.content.state;

			},
			initMap() {
				var that = this;
				this.map = new AMap.Map("container", {
					resizeEnable: true,
					center: [116.478935, 39.997761],
					zoom: 17,
				});
				this.marker = new AMap.Marker({
					icon: new AMap.Icon({
						image: 'https://vehicle-library.oss-cn-beijing.aliyuncs.com/cvbd-icon/track/icon_20220712.png',
						size: new AMap.Size(28, 80),
						imageSize: new AMap.Size(28, 80)
					}),
					map: this.map,
					position: [116.478935, 39.997761],
					// icon: "https://webapi.amap.com/images/car.png",
					// icon:'https://vehicle-library.oss-cn-beijing.aliyuncs.com/cvbd-icon/track/icon_20220712.png',
					offset: new AMap.Pixel(-26, -15),
					autoRotation: true,
					angle: 0,
				});

				// 创建纯文本标记
				var text = new AMap.Text({
					text: `<view
          style="
            width: 140px;
            height: 54px;
            background: #ffffff;
            box-shadow: 0px 2px 4px 0px #e1e1e1;
            box-sizing: border-box;
            padding: 4px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            position: relative;
            border-radius: 40px;
          "
        >
          <view
            style="
              width: 48px;
              height: 48px;
              border: 2px solid #d3e7ff;
              box-sizing: border-box;
              display: flex;
              flex-direction: column;
              justify-content: space-around;
              align-items: center;
              border-radius: 50%;
            "
          >
            <view
              style="
                font-size: 14px;
                font-family: PingFang-SC-Bold, PingFang-SC;
                font-weight: bold;
                color: #1271ff;
              "
              >8.3</view
            >
            <view
              style="
                font-size: 10px;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #1271ff;
                margin-top: -11px;
              "
              >km/h</view
            >
          </view>
          <view
            style="
              font-size: 14px;
              font-family: PingFangSC-Regular, PingFang SC;
              font-weight: 400;
              color: #333333;
              flex: 1;
              margin-left: 6px;
            "
            >赣C765T7</view
          >
        </view>`,
					position: [116.478935, 39.997761],
					offset: new AMap.Pixel(0, -60), //相对于基点的偏移位置
				});

				text.setMap(that.map);
				// let infoContent = `<p class="info-window">时间`;
				// let infoWindow = new AMap.InfoWindow({
				// 	anchor: "bottom-center",
				// 	content: infoContent
				// });
				// infoWindow.open(this.map);

				// 个人定位
				let mapObj = new AMap.Map("iCenter");
				mapObj.plugin("AMap.Geolocation", function() {
					let geolocation = new AMap.Geolocation({
						enableHighAccuracy: true, //是否使用高精度定位,默认:true
						timeout: 2000, //超过10秒后停止定位,默认:无穷大
						maximumAge: 0, //定位结果缓存0毫秒,默认:0
						convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:
						showButton: true, //显示定位按钮,默认:true
						buttonPosition: "LB", //定位按钮停靠位置,默认:'LB',左下角
						buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
						showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
						showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
						panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
						zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
					});
					mapObj.addControl(geolocation);
					geolocation.getCurrentPosition();
					AMap.event.addListener(geolocation, "complete", onComplete); //返回定位信息
					AMap.event.addListener(geolocation, "error", onError); //返回定位出错信息
					function onComplete(data) {
						var p1 = [that.lng, that.lat];
						var p2 = [data.position.lng, data.position.lat];
						var dis = AMap.GeometryUtil.distance(p1, p2);
						that.Kilometers = parseInt(dis / 1000);
					}

					function onError(data) {
						console.log(data);
						that.Kilometers = "";
					}
				});

				// var p1 = [116.434027, 39.941037];
				// var p2 = [116.461665, 39.941564]; // 返回 p1 到 p2 间的地面距离,单位:米
				// var dis = AMap.GeometryUtil.distance(p1, p2);
				// console.log(parseInt(dis) + '米')
				this.map.setFitView();
			},
			// getCenter前往中心点
			getCenter() {
				this.map.setCenter([116.478935, 39.997761])
			},
		},
	};
</script>

<style scoped>
	#container {
		height: 100vh;
		width: 100%;
	}

	.control {
		position: fixed;
		top: 642rpx;
		right: 20rpx;
		z-index: 9999;
		width: 72rpx;
		height: 188rpx;
		background: #ffffff;
		box-shadow: 0px 2rpx 4rpx 0px #e1e1e1;
		border-radius: 6rpx;
		/* opacity: 0.8; */
		box-sizing: border-box;
		padding: 14rpx 14rpx 20rpx 14rpx;
	}

	.split {
		width: 44rpx;
		height: 2rpx;
		border-top: 2rpx solid #d6d6d6;
		margin: 30rpx 0;
	}

	.enlarge,
	.narrow {
		text-align: center;
	}

	.enlarge>image {
		width: 34rpx;
		height: 34rpx;
	}

	.narrow>image {
		width: 34rpx;
		height: 6rpx;
	}

	.location {
		width: 88rpx;
		height: 88rpx;
		position: fixed;
		right: 16rpx;
		bottom: 292rpx;
	}

	.location>image {
		width: 88rpx;
		height: 88rpx;
	}

	/* 顶部展示 */
	.controlLeftView {
		position: absolute;
		top: 100rpx;
		left: 20rpx;
		width: 300rpx;
	}

	.controlLeft {
		width: 100%;
		height: 64rpx;
		background: #ffffff;
		box-shadow: 0px 4rpx 8rpx 0px #e1e1e1;
		border-radius: 37rpx;
		margin-top: 20rpx;
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
		line-height: 64rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
	}

	/* 底部展示 */
	.controlLeftBottomView {
		width: 96%;
		position: absolute;
		left: 2%;
		bottom: 40rpx;
		height: 180rpx;
		background: #ffffff;
		box-shadow: 0px 4rpx 8rpx 0px #e1e1e1;
		border-radius: 12rpx;
		box-sizing: border-box;
		padding: 34rpx 24rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.controlLeftBottomView>.flex {
		display: flex;
		width: 100%;
		justify-content: space-between;
		align-items: center;
	}

	.controlLeftBottomView>.flex>.title {
		width: 74rpx;
		height: 36rpx;
		box-shadow: 0px 4rpx 8rpx 0px #e1e1e1;
		border-radius: 18rpx;
		border: 2rpx solid #cccccc;
		font-size: 22rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #a8a8a8;
		text-align: center;
		line-height: 36rpx;
	}

	.controlLeftBottomView>.flex>.time {
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
		text-shadow: 0px 4rpx 8rpx #e1e1e1;
		flex: 1;
		box-sizing: border-box;
		padding-left: 16rpx;
	}

	.controlLeftBottomView>.flex>.mi {
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
		text-shadow: 0px 4rpx 8rpx #e1e1e1;
	}

	.controlLeftBottomView>.flex>image {
		width: 32rpx;
		height: 32rpx;
		/* background: #000000; */
		/* box-shadow: 0px 4rpx 8rpx 0px #e1e1e1; */
	}

	.controlLeftBottomView>.flex>.city {
		flex: 1;
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #333333;
		text-shadow: 0px 4rpx 8rpx #e1e1e1;
		box-sizing: border-box;
		padding-left: 16rpx;
	}

	>>>.amap-overlay-text-container {
		border: none;
		background: none;
		overflow: inherit;
	}
</style>

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一份uniapp高德地图轨迹代码,供您参考: ``` <template> <view class="map-container"> <map :longitude="longitude" :latitude="latitude" :markers="markers" :polyline="polyline" :include-points="true" @updated="onMapUpdate"></map> <button @click="startRecord" v-if="!isRecording">开始记录</button> <button @click="stopRecord" v-if="isRecording">停止记录</button> </view> </template> <script> export default { data() { return { longitude: 0, // 经度 latitude: 0, // 纬度 markers: [], // 标记点 polyline: [], // 折线 isRecording: false, // 是否正在记录轨迹 recordTimer: null, // 记录轨迹的计时器 recordPoints: [], // 记录的轨迹点 }; }, methods: { // 开始记录轨迹 startRecord() { this.isRecording = true; this.recordPoints = []; // 清空之前的轨迹点 this.recordTimer = setInterval(() => { // 获取当前位置 uni.getLocation({ type: 'gcj02', success: (res) => { const { longitude, latitude } = res; this.longitude = longitude; this.latitude = latitude; // 添加轨迹点 this.recordPoints.push({ longitude, latitude, }); // 更新标记点和折线 this.markers = [{ id: 1, longitude, latitude, iconPath: '/static/location.png', width: 30, height: 30, }]; this.polyline = [{ points: this.recordPoints, color: '#FF0000DD', width: 4, dottedLine: false, }]; }, }); }, 1000); }, // 停止记录轨迹 stopRecord() { this.isRecording = false; clearInterval(this.recordTimer); }, // 地图更新事件 onMapUpdate(e) { console.log('地图更新事件:', e); }, }, }; </script> <style scoped> .map-container { width: 100%; height: 100%; position: relative; } button { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); padding: 10px 20px; border: none; border-radius: 4px; background-color: #009688; color: white; } </style> ``` 在上述代码中,我们使用了 uniapp 的 map 组件来显示地图,并通过高德地图提供的 uni.getLocation 接口来获取当前位置。在开始记录轨迹时,我们使用 setInterval 定时获取位置信息,并将获取到的位置信息添加到轨迹点中,同时更新地图上的标记点和折线。在停止记录轨迹时,我们清空计时器并停止记录。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值