6月-高德地图绘制指南
- 高德根据起始点经纬度获取途经点绘制路段
- 高德绘制路线、路线悬浮、点击变色
- 根据经纬度获取地址信息
- 行政区域加边界线
高德根据起始点经纬度获取途经点绘制路段
const routeDriveInstance = new AMap.Driving({
policy: 0,
hideMarkers: true,
});
const path = [[30.233,120.389],[31.233,120.189],....];
const startPoint = path[0]
const endPoint = path[path.length - 1];
routeDriveInstance.search(startPoint, endPoint, (status, result) => {
if (status === "complete" && result.info === "OK") {
let path = [];
result.routes[0].steps.map((o) => {
o.path.map((m) => {
path.push([m.lng, m.lat]);
});
});
this.line = new AMap.Polyline(path);
高德绘制路线、路线悬浮、点击变色
this.line = new AMap.Polyline(
path,
{
strokeColor: '#4F9AFF',
strokeOpacity: 0.65,
strokeWeight: 5,
showDir: false,
isOutline: false,
},
obj:'...'
);
this.line.on("mouseover", (e) => {
e.target.setOptions({
strokeOpacity: 1,
strokeWeight: 7,
isOutline: false,
});
});
this.line.on("mouseout", (e) => {
e.target.setOptions({
strokeOpacity: 0.65,
strokeWeight: 5,
isOutline: false,
});
});
this.line.on("click", (e) => {
e.target.setOptions({
isOutline: true,
outlineColor: 'rgba(79, 154, 255,.3)',
borderWeight: 5,
});
});
根据经纬度获取地址信息
this.getAdcodeByCoor([longitude, latitude]).then((item) => {
console.log(item.adress)
});
getAdcodeByCoor(coor) {
return new Promise((resolve) => {
this.geocoder.getAddress(coor, (status, result) => {
if (status === "complete" && result.info === "OK") {
const { regeocode } = result;
if (regeocode.addressComponent) {
const { district, township } = regeocode.addressComponent;
const adress =
regeocode.formattedAddress.split(township ? township : district)[1] || township;
resolve({ adress});
}
} else {
resolve({});
}
});
});
},
行政区域加边界线
const level = this.getZoomLevel({ adcode: appConfig.mainAdcode + "" });
const districtSearch = new AMap.DistrictSearch({
extensions: "all",
level:
level === 7 ? "province" : level === 10 ? "city" : level === 12 ? "district" : "street",
});
const mainAdcode =510100
districtSearch.search(mainAdcode + "", (status, result) => {
if (status === "complete") {
const bounds = result.districtList[0].boundaries;
let polygons = [];
if (bounds) {
for (var i = 0, l = bounds.length; i < l; i++) {
var polygonLevel = new AMap.Polyline({
strokeWeight: 5,
path: bounds[i],
strokeColor: "#0091ea",
strokeStyle: "solid",
});
polygons.push(polygonLevel);
}
}
this.map.add(polygons);
this.map.setFitView(polygons);
}
});
getZoomLevel({ adcode, typecode }) {
if (adcode === "100000") return 4;
if (adcode.slice(2, 6) === "0000") return 7;
if (adcode.slice(4, 6) === "00") return 10;
if (adcode.slice(4, 6) !== "00" && !typecode) return 12;
if (typecode === "190105") return 12;
if (typecode === "190106") return 14;
if (typecode === "190108") return 15;
if (typecode === "190109" || typecode === "190107") return 16;
if (typecode > 190218 || typecode < 190100) return 17;
return 12;
},