高德地图api

1amap-uni.js

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;

2lyn4ever-gaode.js

//高德key 注意 此 key 为小程序为key
const key = '你的key';
 
const amapFile = require('./amap-uni.js');
/*
调用高德地图api进行路线规划时,
高德对途经点的坐标格式如下:
 
116.441063,39.91903;
116.39622,39.912057;
116.39622,39.912057;
116.39622,39.912057;
116.39622,39.912057;
116.39622,39.912057;
116.39622,39.912057;
116.39622,39.912057
 
也就是说每个点的经纬度之间用;分隔,所以请提前格式化好您的坐标格式
 
*/
 
 
function PlanningRoute(start, end, _waypoints, result, fail) {
	let that = this;
	var myAmapFun = new amapFile.AMapWX({
		key: key
	});
 
	myAmapFun.getDrivingRoute({
		origin: start,
		destination: end,
		waypoints: _waypoints,
		success: function (data) {
			var points = [];
			if (data.paths && data.paths[0] && data.paths[0].steps) {
				var steps = data.paths[0].steps;
				for (var i = 0; i < steps.length; i++) {
					var poLen = steps[i].polyline.split(';');
					for (var j = 0; j < poLen.length; j++) {
						points.push({
							longitude: parseFloat(poLen[j].split(',')[0]),
							latitude: parseFloat(poLen[j].split(',')[1])
						})
					}
				}
			}
			//这个返回结果就是对应的路线坐标,其他属性页面自己配置,请参照uniapp地图组件一章节
			//https://uniapp.dcloud.io/component/map
			result({
				points: points,
				color: "#0606ff",
				width: 8
			})
		},
		fail: function (info) {
			fail(info)
		}
	})
}
//标记标记点
function Makemarkers(startpoi, endpoi, waypoints, success) {
	let markers = [];
 
	//起点
	let start = {
		iconPath: "/static/marker-start.png",
		id: 0,
		longitude: startpoi.split(",")[0],
		latitude: startpoi.split(",")[1],
		width: 23,
		height: 33
	}
	markers.push(start)
	//终点
	let end = {
		iconPath: "/static/marker-end.png",
		id: 1,
		longitude: endpoi.split(",")[0],
		latitude: endpoi.split(",")[1],
		width: 23,
		height: 33
	}
	markers.push(end)
	//途经点,先将其分隔成为数组
	let _waypoints = waypoints.split(';')
	for (let i = 0, _len = _waypoints.length; i < _len; i++) {
		let point = {
			iconPath: "/static/tjd.png",
			id: i,
			longitude: parseFloat(_waypoints[i].split(",")[0]),
			latitude: parseFloat(_waypoints[i].split(",")[1]),
			width: 23,
			height: 33
		}
		markers.push(point)
	}
 
	//统一风格为回调方式,也可以直接返回这个markers
	success(markers);
 
}
 
module.exports = {
	line: PlanningRoute,
	markers: Makemarkers
}

3

<template>
  <view>
    <map
          style="width: 100vw; height: 60vh"
          :latitude="latitude"
          :longitude="longitude"
          :markers="markers"
          :polyline="polyline"
          scale="16"
        >
    </map>
  </view>
</template>
 
<script>
import Amap from "../../utils/lyn4ever-gaode.js";
export default {
  name: "UniappRealTimeMonitoringMap",
  data() {
    return {
      longitude: 108.921672,
      latitude: 34.250646,
      markers: [],
      polyline: [],
    }
  },
  onLoad() {
      let that = this;
      var startPoi = "108.921672,34.250646";
      // 途径点
      var wayPoi = "108.936799,34.245842;108.973306,34.244767;109.013335,34.249303";
      var wayPoi = "";
      var endPoi = "109.033673,34.252884";
  
      Amap.line(startPoi, endPoi, wayPoi, function (res) {
        console.log(res);
        that.polyline.push(res);
      });
  
      Amap.markers(startPoi, endPoi, wayPoi, function (res) {
        // console.log(res);
        that.markers.push.apply(that.markers, res);
      });
  }
}
</script>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
高德地图API 2.0是一个开放平台,提供开发者使用的地图相关功能接口。 首先,为了使用高德地图API 2.0,我们需要在高德开放平台上创建一个开发者账号,并且申请一个API Key。API Key是使用API的身份标识,每个开发者账号可以申请多个API Key。 然后,我们可以使用API Key在开发项目中调用高德地图API的各种功能接口。这些接口包括地图显示、地图搜索、路径规划、地理编码等等。 例如,如果我们想在网页上显示一个地图,可以使用JavaScript API。首先,在网页中引入高德地图APIJavaScript文件,并在HTML中创建一个指定位置的容器,然后在JavaScript代码中调用地图显示接口,传入容器和配置参数,即可在网页上显示地图。 另外,如果我们需要实现地图搜索功能,可以使用地理编码和输入提示接口。通过地理编码接口,我们可以将地名转换为经纬度坐标,通过输入提示接口,可以根据用户输入的关键字,提供相应的地点建议。 同时,高德地图API还提供了路径规划接口,可以实现驾车、步行、公交等不同出行方式的路径规划功能。 除了以上功能,高德地图API还提供了丰富的地图样式、地图覆盖物、地图交互等等功能接口,供开发者根据自己的需求自由调用和定制。 总之,高德地图API 2.0使用方便、功能强大,可以帮助开发者在自己的项目中轻松实现各种地图相关的功能。无论是网页、移动应用还是其他项目,高德地图API都是一个值得推荐的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑猫大人-魏盛楠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值