一、获取高德Key:
官方获取key的方法在这

二、创建项目之后下载并安装微信小程序SKD:
如果是开发安卓、IOS、APP项目,请找到对应的包下载并解压。
下载微信小程序SDK

解压后得到 amap-wx.js 文件,在创建的项目中,新建一个名为 libs 目录,将 amap-wx.js 文件拷贝到 libs 的本地目录下,完成安装。
这一步在uniapp运行的时候会报错,因为解压后的js文件方法都是wx.XXX。手动把这些前缀都改成uni.XXX之后可以运行了。
以下是修改之后的文件:

function AMapWX(a) {
	this.key = a.key, this.requestConfig = {
		key: a.key,
		s: "rsx",
		platform: "WXJS",
		appname: a.key,
		sdkversion: "1.2.0",
		logversion: "2.0"
	}
}
AMapWX.prototype.getWxLocation = function(a, b) {
	uni.getLocation({
		type: "gcj02",
		success: function(a) {
			var c = a.longitude + "," + a.latitude;
			uni.setStorage({
				key: "userLocation",
				data: c
			}), b(c)
		},
		fail: function(c) {
			uni.getStorage({
				key: "userLocation",
				success: function(a) {
					a.data && b(a.data)
				}
			}), a.fail({
				errCode: "0",
				errMsg: c.errMsg || ""
			})
		}
	})
}, AMapWX.prototype.getRegeo = function(a) {
	function c(c) {
		var d = b.requestConfig;
		uni.request({
			url: "https://restapi.amap.com/v3/geocode/regeo",
			data: {
				key: b.key,
				location: c,
				extensions: "all",
				s: d.s,
				platform: d.platform,
				appname: b.key,
				sdkversion: d.sdkversion,
				logversion: d.logversion
			},
			method: "GET",
			header: {
				"content-type": "application/json"
			},
			success: function(b) {
				var d, e, f, g, h, i, j, k, l;
				b.data.status && "1" == b.data.status ? (d = b.data.regeocode, e = d.addressComponent, f = [], g = "", d && d.roads[
						0] && d.roads[0].name && (g = d.roads[0].name + "附近"), h = c.split(",")[0], i = c.split(",")[1], d.pois && d
					.pois[0] && (g = d.pois[0].name + "附近", j = d.pois[0].location, j && (h = parseFloat(j.split(",")[0]), i =
						parseFloat(j.split(",")[1]))), e.provice && f.push(e.provice), e.city && f.push(e.city), e.district && f.push(
						e.district), e.streetNumber && e.streetNumber.street && e.streetNumber.number ? (f.push(e.streetNumber.street),
						f.push(e.streetNumber.number)) : (k = "", d && d.roads[0] && d.roads[0].name && (k = d.roads[0].name), f.push(
						k)), f = f.join(""), l = [{
						iconPath: a.iconPath,
						width: a.iconWidth,
						height: a.iconHeight,
						name: f,
						desc: g,
						longitude: h,
						latitude: i,
						id: 0,
						regeocodeData: d
					}], a.success(l)) : a.fail({
					errCode: b.data.infocode,
					errMsg: b.data.info
				})
			},
			fail: function(b) {
				a.fail({
					errCode: "0",
					errMsg: b.errMsg || ""
				})
			}
		})
	}
	var b = this;
	a.location ? c(a.location) : b.getWxLocation(a, function(a) {
		c(a)
	})
}, AMapWX.prototype.getWeather = function(a) {
	function d(d) {
		var e = "base";
		a.type && "forecast" == a.type && (e = "all"), uni.request({
			url: "https://restapi.amap.com/v3/weather/weatherInfo",
			data: {
				key: b.key,
				city: d,
				extensions: e,
				s: c.s,
				platform: c.platform,
				appname: b.key,
				sdkversion: c.sdkversion,
				logversion: c.logversion
			},
			method: "GET",
			header: {
				"content-type": "application/json"
			},
			success: function(b) {
				function c(a) {
					var b = {
						city: {
							text: "城市",
							data: a.city
						},
						weather: {
							text: "天气",
							data: a.weather
						},
						temperature: {
							text: "温度",
							data: a.temperature
						},
						winddirection: {
							text: "风向",
							data: a.winddirection + "风"
						},
						windpower: {
							text: "风力",
							data: a.windpower + "级"
						},
						humidity: {
							text: "湿度",
							data: a.humidity + "%"
						}
					};
					return b
				}
				var d, e;
				b.data.status && "1" == b.data.status ? b.data.lives ? (d = b.data.lives, d && d.length > 0 && (d = d[0], e = c(
					d), e["liveData"] = d, a.success(e))) : b.data.forecasts && b.data.forecasts[0] && a.success({
					forecast: b.data.forecasts[0]
				}) : a.fail({
					errCode: b.data.infocode,
					errMsg: b.data.info
				})
			},
			fail: function(b) {
				a.fail({
					errCode: "0",
					errMsg: b.errMsg || ""
				})
			}
		})
	}

	function e(e) {
		uni.request({
			url: "https://restapi.amap.com/v3/geocode/regeo",
			data: {
				key: b.key,
				location: e,
				extensions: "all",
				s: c.s,
				platform: c.platform,
				appname: b.key,
				sdkversion: c.sdkversion,
				logversion: c.logversion
			},
			method: "GET",
			header: {
				"content-type": "application/json"
			},
			success: function(b) {
				var c, e;
				b.data.status && "1" == b.data.status ? (e = b.data.regeocode, e.addressComponent ? c = e.addressComponent.adcode :
					e.aois && e.aois.length > 0 && (c = e.aois[0].adcode), d(c)) : a.fail({
					errCode: b.data.infocode,
					errMsg: b.data.info
				})
			},
			fail: function(b) {
				a.fail({
					errCode: "0",
					errMsg: b.errMsg || ""
				})
			}
		})
	}
	var b = this,
		c = b.requestConfig;
	a.city ? d(a.city) : b.getWxLocation(a, function(a) {
		e(a)
	})
}, AMapWX.prototype.getPoiAround = function(a) {
	function d(d) {
		var e = {
			key: b.key,
			location: d,
			s: c.s,
			platform: c.platform,
			appname: b.key,
			sdkversion: c.sdkversion,
			logversion: c.logversion
		};
		a.querytypes && (e["types"] = a.querytypes), a.querykeywords && (e["keywords"] = a.querykeywords), uni.request({
			url: "https://restapi.amap.com/v3/place/around",
			data: e,
			method: "GET",
			header: {
				"content-type": "application/json"
			},
			success: function(b) {
				var c, d, e, f;
				if (b.data.status && "1" == b.data.status) {
					if (b = b.data, b && b.pois) {
						for (c = [], d = 0; d < b.pois.length; d++) e = 0 == d ? a.iconPathSelected : a.iconPath, c.push({
							latitude: parseFloat(b.pois[d].location.split(",")[1]),
							longitude: parseFloat(b.pois[d].location.split(",")[0]),
							iconPath: e,
							width: 22,
							height: 32,
							id: d,
							name: b.pois[d].name,
							address: b.pois[d].address
						});
						f = {
							markers: c,
							poisData: b.pois
						}, a.success(f)
					}
				} else a.fail({
					errCode: b.data.infocode,
					errMsg: b.data.info
				})
			},
			fail: function(b) {
				a.fail({
					errCode: "0",
					errMsg: b.errMsg || ""
				})
			}
		})
	}
	var b = this,
		c = b.requestConfig;
	a.location ? d(a.location) : b.getWxLocation(a, function(a) {
		d(a)
	})
}, AMapWX.prototype.getStaticmap = function(a) {
	function f(b) {
		c.push("location=" + b), a.zoom && c.push("zoom=" + a.zoom), a.size && c.push("size=" + a.size), a.scale && c.push(
				"scale=" + a.scale), a.markers && c.push("markers=" + a.markers), a.labels && c.push("labels=" + a.labels), a.paths &&
			c.push("paths=" + a.paths), a.traffic && c.push("traffic=" + a.traffic);
		var e = d + c.join("&");
		a.success({
			url: e
		})
	}
	var e, b = this,
		c = [],
		d = "https://restapi.amap.com/v3/staticmap?";
	c.push("key=" + b.key), e = b.requestConfig, c.push("s=" + e.s), c.push("platform=" + e.platform), c.push("appname=" +
			e.appname), c.push("sdkversion=" + e.sdkversion), c.push("logversion=" + e.logversion), a.location ? f(a.location) :
		b.getWxLocation(a, function(a) {
			f(a)
		})
}, AMapWX.prototype.getInputtips = function(a) {
	var b = this,
		c = b.requestConfig,
		d = {
			key: b.key,
			s: c.s,
			platform: c.platform,
			appname: b.key,
			sdkversion: c.sdkversion,
			logversion: c.logversion
		};
	a.location && (d["location"] = a.location), a.keywords && (d["keywords"] = a.keywords), a.type && (d["type"] = a.type),
		a.city && (d["city"] = a.city), a.citylimit && (d["citylimit"] = a.citylimit), uni.request({
			url: "https://restapi.amap.com/v3/assistant/inputtips",
			data: d,
			method: "GET",
			header: {
				"content-type": "application/json"
			},
			success: function(b) {
				b && b.data && b.data.tips && a.success({
					tips: b.data.tips
				})
			},
			fail: function(b) {
				a.fail({
					errCode: "0",
					errMsg: b.errMsg || ""
				})
			}
		})
}, AMapWX.prototype.getDrivingRoute = function(a) {
	var b = this,
		c = b.requestConfig,
		d = {
			key: b.key,
			s: c.s,
			platform: c.platform,
			appname: b.key,
			sdkversion: c.sdkversion,
			logversion: c.logversion
		};
	a.origin && (d["origin"] = a.origin),
		a.destination && (d["destination"] = a.destination),
		a.strategy && (d["strategy"] = a.strategy),
		a.waypoints && (d["waypoints"] = a.waypoints),
		a.avoidpolygons && (d["avoidpolygons"] = a.avoidpolygons),
		a.avoidroad && (d["avoidroad"] = a.avoidroad),
		uni.request({
			url: "https://restapi.amap.com/v3/direction/driving",
			data: d,
			method: "GET",
			header: {
				"content-type": "application/json"
			},
			success: function(b) {
				b && b.data && b.data.route && a.success({
					paths: b.data.route.paths,
					taxi_cost: b.data.route.taxi_cost || ""
				})
			},
			fail: function(b) {
				a.fail({
					errCode: "0",
					errMsg: b.errMsg || ""
				})
			}
		})
}, AMapWX.prototype.getWalkingRoute = function(a) {
	var b = this,
		c = b.requestConfig,
		d = {
			key: b.key,
			s: c.s,
			platform: c.platform,
			appname: b.key,
			sdkversion: c.sdkversion,
			logversion: c.logversion
		};
	a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), uni.request({
		url: "https://restapi.amap.com/v3/direction/walking",
		data: d,
		method: "GET",
		header: {
			"content-type": "application/json"
		},
		success: function(b) {
			b && b.data && b.data.route && a.success({
				paths: b.data.route.paths
			})
		},
		fail: function(b) {
			a.fail({
				errCode: "0",
				errMsg: b.errMsg || ""
			})
		}
	})
}, AMapWX.prototype.getTransitRoute = function(a) {
	var b = this,
		c = b.requestConfig,
		d = {
			key: b.key,
			s: c.s,
			platform: c.platform,
			appname: b.key,
			sdkversion: c.sdkversion,
			logversion: c.logversion
		};
	a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), a.strategy && (d[
		"strategy"] = a.strategy), a.city && (d["city"] = a.city), a.cityd && (d["cityd"] = a.cityd), uni.request({
		url: "https://restapi.amap.com/v3/direction/transit/integrated",
		data: d,
		method: "GET",
		header: {
			"content-type": "application/json"
		},
		success: function(b) {
			if (b && b.data && b.data.route) {
				var c = b.data.route;
				a.success({
					distance: c.distance || "",
					taxi_cost: c.taxi_cost || "",
					transits: c.transits
				})
			}
		},
		fail: function(b) {
			a.fail({
				errCode: "0",
				errMsg: b.errMsg || ""
			})
		}
	})
}, AMapWX.prototype.getRidingRoute = function(a) {
	var b = this,
		c = b.requestConfig,
		d = {
			key: b.key,
			s: c.s,
			platform: c.platform,
			appname: b.key,
			sdkversion: c.sdkversion,
			logversion: c.logversion
		};
	a.origin && (d["origin"] = a.origin), a.destination && (d["destination"] = a.destination), uni.request({
		url: "https://restapi.amap.com/v4/direction/bicycling",
		data: d,
		method: "GET",
		header: {
			"content-type": "application/json"
		},
		success: function(b) {
			b && b.data && b.data.data && a.success({
				paths: b.data.data.paths
			})
		},
		fail: function(b) {
			a.fail({
				errCode: "0",
				errMsg: b.errMsg || ""
			})
		}
	})
}, module.exports.AMapWX = AMapWX;

三、SDK导入成功后就可以开始使用了:
1、在需要使用高德地图的vue文件中引入 amap-wx.js 文件。

import amap from '@/common/amap-uni.js';

然后,在.vue中实例化 AMapWX 对象,调用 getPoiAround 方法,获取POI数据。

为保证 marker 以自定义的图标显示,需在给marker一个可以访问到的图片地址(选中状态和未选中状态两张,本地或链接都可以)
以下是完整代码:

	<!-- /**  
 * map 用到的属性 
 * @param width map宽度
 * @param height map高度
 * @param latitude 中心纬度
 * @param longitude 中心经度
 * @param scale 缩放级别,取值范围为5-18
 * @param markers 标记点 
 * @param show-location 显示带有方向的当前定位点
 * @param markertap 点击标记点时触发
 * */ -->
<view class="content">
	<map class="map" id="map" :latitude="latitude" :longitude="longitude" scale="18" show-location="true" :markers="markers"
	 @markertap="markertap"></map>
</view>
import amap from '@/common/amap-uni.js';
export default {
	data() {
		return {
			amapPlugin: null,
			key: '5466',//高德地图key
			markers: [],
			poisdatas: [],
			latitude: 30.208487,
			longitude: 120.21202,
		}
	},
	onLoad() {
		var that = this;
		this.amapPlugin = new amap.AMapWX({
			key: this.key
		});
		this.amapPlugin.getPoiAround({
			iconPathSelected: '', //如:..­/..­/img/marker_checked.png
			iconPath: '', //如:..­/..­/img/marker.png
			success: function(data) {
				//成功回调
				that.markers = data.markers;
				that.latitude = that.markers[0].latitude;
				that.longitude = that.markers[0].longitude;
				// that.showMarkerInfo(that.markers,0);
				
				that.poisdatas = data.poisData;
				var markers_new = [];
				that.markers.forEach(function(item, index) {
					markers_new.push({
						id: item.id, //marker 序号
						width: item.width, //marker 宽度
						height: item.height, //marker 高度
						iconPath: item.iconPath, //marker 图标路径
						latitude: item.latitude, //marker  纬度
						longitude: item.longitude, //marker 经度
						address: item.address,//marker 地址
						name: item.name,//marker 名字
						// title:item.name,//标注点名
						distance: that.poisdatas[index].distance,//marker 距离中心点之间的距离 mark属性中并没有这个字段,我是因为项目需求需要知道标点跟中心点之间的距离,所以塞了一个字段进去。
						//自定义标记点上方的气泡窗口
						callout: {
							padding: 2, //callout 文本边缘留白
							fontSize: 15, //callout  文字大小
							bgColor: 'blue', //callout 背景颜色
							color: '#6B8E23', //callout 文字颜色
							borderRadius: 5, //边框圆角
							display: 'BYCLICK', //callout 'BYCLICK':点击显示; 'ALWAYS':常显
							content: that.poisdatas[index].name //地理位置名称
						}
					})
				})
				that.markers = markers_new;
			},
			fail: function(info) {
				//失败回调
				console.log("info", info)
			}
		})
	},
	methods: {
		jump(path, query) {
			this.$Router.push({
				path: path,
				query: {item:JSON.stringify(query)}
			});
		},
		//点击地图标点时触发
		markertap: function(e) {
			var id = e.markerId;
			var that = this;
			that.changeMarkerColor(that.markers,id);
		},
		changeMarkerColor: function(data, i) {
			var that = this;
			var markers = [];
			for (var j = 0; j < data.length; j++) {
				if (j == i) {
					data[j].iconPath = ""; //如:..­/..­/img/marker_checked.png
				} else {
					data[j].iconPath = ""; //如:..­/..­/img/marker.png
				}
				markers.push(data[j]);
			}
			that.marker = markers;
		}
	}
}

效果图如下:
在这里插入图片描述
可参考的官方链接如下:
高德地图微信小程序版开发指南
uniapp的map组件
uniapp的map相关API

注:map是原生组件,层级高于前端组件,如果需要在map上覆盖什么,除了用map自带的控件之外,只能用view-cover。详细请查看uniapp官网

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐