js高德地图添加点Marker,添加线段Polyline,添加一个区域Polygon(面)
高德地图JS API 实例 亲测可用
参考网站=> 阿里云数据可视化平台(下载json用的):http://datav.aliyun.com/portal/school/atlas/area_selector?spm=a2crr.23498931.0.0.685915dd8QQdlv
1.渲染地图
const [initDataMap, setInitDataMap] = useState({
centerCity: '拱墅区',
defaultZoom: 12,
centerPoint: { lng: 120.165533, lat: 30.329062 },
});
//初始化地图
const initMap = () => {
const { centerPoint } = initDataMap;
const center = [centerPoint.lng, centerPoint.lat];
const mzooms = [8, 19];
const mzoom = 12;
let map = new AMap.Map("AMapBox", {
zoom: mzoom, //初始化地图层级
zooms: mzooms,
rotateEnable: false, // 固定视角
disableSocket: true,
center: center,
});
mapRef.current = map;
addAreaCoordinate(map); // 这个是渲染块
};
2.绘制Marker标记点
// 绘制点
const drawMarker = (data: any, map: any) => {
const infoWindow = new AMap.InfoWindow({
offset: new AMap.Pixel(5, -30),
autoMove: true,
closeWhenClickMap: true,
});
let ap: any = []
data.forEach((item: any) => {
if (item.lat && item.lng) {
const ad = [item.lng, item.lat];
const marker = new AMap.Marker({
position: ad,
icon: iconIMg, // 自己的icon
map: map
});
ap.push(marker);
setMarkerList(ap);
const content = item.projectName;
marker.on('click', () => {
infoWindow.setContent(content);
infoWindow.open(map, ad);
});
}
});
map.setFitView();
}
3.绘制线段Polyline
// 绘制线段
const polylineInit = (lineArr: any, map: any, callBack: any) => {
const infoWindowLine = new AMap.InfoWindow({
offset: new AMap.Pixel(5, -30),
autoMove: true,
closeWhenClickMap: true,
});
const polyline = new AMap.Polyline({
path: lineArr.list, //设置线覆盖物路径
strokeColor: "#3366FF", //线颜色
strokeOpacity: 1, //线透明度
strokeWeight: 5, //线宽
strokeStyle: "solid", //线样式
strokeDasharray: [10, 5] //补充线样式
});
polyline.setMap(map);
callBack(polyline);
const content = `
<div>
<div style='border-bottom: 1px solid #F0F0F0; margin-bottom: 4px; padding: 4px 0 4px 0; color: #000000; font-size: 16px; '>${lineArr.roadName}</div>
<div >所属国企:${lineArr.belongCorpName}</div>
<div>当前进度:${lineArr.currentStatusStr}</div>
<a onclick="handleClickDetail(${lineArr.id})">查看详情信息</a>
<div>
`
if (callBackDetail) {
polyline.on('click', (e: any) => {
infoWindowLine.setContent(content);
infoWindowLine.open(map, e.lnglat);
});
}
}
// 处理绘制线段 可不看嘎嘎···
const dealPolylineInit = (arr: any, map: any) => {
// map.clearMap();
map.remove(polylineList);
let ad: any = [];
arr.forEach((item: any) => {
const st = JSON.parse(item.locationMark);
st.forEach((element: any) => {
element.forEach((ele: any) => {
ele.roadName = item.roadName;
ele.belongCorpName = item.belongCorpName;
ele.currentStatusStr = item.currentStatusStr;
ele.id = item.roadId;
});
});
ad.push(st);
});
const flatArr = ad.flat();
const cloneDeepData = cloneDeep(flatArr);
const opd: any = [];
cloneDeepData.forEach((item: any) => {
let lineArr: any = [];
const obj: any = {};
item.forEach((element: any) => {
const ad = [element.lng, element.lat];
obj.roadName = element.roadName;
obj.belongCorpName = element.belongCorpName;
obj.currentStatusStr = element.currentStatusStr;
obj.id = element.id
lineArr.push(ad);
});
obj.list = lineArr;
polylineInit(obj, map, (v: any) => {
opd.push(v)
});
})
setPolylineList(opd)
}
4.绘制区域Polygon
const addAreaCoordinate = (map: any) => {
const obj = gs_json || '';
const points: any[] = [];
obj?.features[0]?.geometry?.coordinates[0][0].map((item: any) => {
points.push(new AMap.LngLat(item[0], item[1]));
});
const polygon = new AMap.Polygon({
path: points,
color: '#1CB9FF',
weight: 3,
opacity: 0.5,
fillColor: '#1CB9FF',
fillOpacity: 0.05,
});
map.add(polygon);
map.setFitView(polygon);//视口自适应
}
5.完整的代码------(react写的,但不影响cv)
import React, { useRef, forwardRef, useImperativeHandle, useEffect, useState } from 'react';
//antd
// 第三方组件
//@ts-ignore
import AMap from 'AMap';
import { cloneDeep } from 'lodash';
import gs_json from '@/assets/json/gongshu.json'; // 地图区域的json数据
import iconIMg from '@/assets/productizationimg/local.png'
const AMapModal = forwardRef((props: any, ref: any) => {
const { roadMapData, projectMapData, isShowLanLat, callBackDetail } = props;
const mapRef = useRef<any>();
const [markerList, setMarkerList] = useState<any>([]);
const [polylineList, setPolylineList] = useState<any>([]);
const [initDataMap, setInitDataMap] = useState({
centerCity: '拱墅区',
defaultZoom: 12,
centerPoint: { lng: 120.165533, lat: 30.329062 },
});
//@ts-ignore
window.document.handleClickDetail = function (id: any) {
if (callBackDetail) {
callBackDetail(id);
}
};
// 根据levelCode向地图中画一个区域轮廓
const addAreaCoordinate = (map: any) => {
const obj = gs_json || '';
const points: any[] = [];
obj?.features[0]?.geometry?.coordinates[0][0].map((item: any) => {
points.push(new AMap.LngLat(item[0], item[1]));
});
const polygon = new AMap.Polygon({
path: points,
color: '#1CB9FF',
weight: 3,
opacity: 0.5,
fillColor: '#1CB9FF',
fillOpacity: 0.05,
});
map.add(polygon);
map.setFitView(polygon);//视口自适应
}
// 绘制点
const drawMarker = (data: any, map: any) => {
const infoWindow = new AMap.InfoWindow({
offset: new AMap.Pixel(5, -30),
autoMove: true,
closeWhenClickMap: true,
});
let ap: any = []
data.forEach((item: any) => {
if (item.lat && item.lng) {
const ad = [item.lng, item.lat];
const marker = new AMap.Marker({
position: ad,
icon: iconIMg,
map: map
});
ap.push(marker);
setMarkerList(ap);
const content = item.projectName;
marker.on('click', () => {
infoWindow.setContent(content);
infoWindow.open(map, ad);
});
}
});
map.setFitView();
}
// 绘制线段
const polylineInit = (lineArr: any, map: any, callBack: any) => {
const infoWindowLine = new AMap.InfoWindow({
offset: new AMap.Pixel(5, -30),
autoMove: true,
closeWhenClickMap: true,
});
const polyline = new AMap.Polyline({
path: lineArr.list, //设置线覆盖物路径
strokeColor: "#3366FF", //线颜色
strokeOpacity: 1, //线透明度
strokeWeight: 5, //线宽
strokeStyle: "solid", //线样式
strokeDasharray: [10, 5] //补充线样式
});
polyline.setMap(map);
callBack(polyline);
const content = `
<div>
<div style='border-bottom: 1px solid #F0F0F0; margin-bottom: 4px; padding: 4px 0 4px 0; color: #000000; font-size: 16px; '>${lineArr.roadName}</div>
<div >所属国企:${lineArr.belongCorpName}</div>
<div>当前进度:${lineArr.currentStatusStr}</div>
<a onclick="handleClickDetail(${lineArr.id})">查看详情信息</a>
<div>
`
if (callBackDetail) {
polyline.on('click', (e: any) => {
infoWindowLine.setContent(content);
infoWindowLine.open(map, e.lnglat);
});
}
}
// 处理绘制线段
const dealPolylineInit = (arr: any, map: any) => {
// map.clearMap();
map.remove(polylineList); // 清除线段的
let ad: any = [];
arr.forEach((item: any) => {
const st = JSON.parse(item.locationMark);
st.forEach((element: any) => {
element.forEach((ele: any) => {
ele.roadName = item.roadName;
ele.belongCorpName = item.belongCorpName;
ele.currentStatusStr = item.currentStatusStr;
ele.id = item.roadId;
});
});
ad.push(st);
});
const flatArr = ad.flat();
const cloneDeepData = cloneDeep(flatArr);
const opd: any = [];
cloneDeepData.forEach((item: any) => {
let lineArr: any = [];
const obj: any = {};
item.forEach((element: any) => {
const ad = [element.lng, element.lat];
obj.roadName = element.roadName;
obj.belongCorpName = element.belongCorpName;
obj.currentStatusStr = element.currentStatusStr;
obj.id = element.id
lineArr.push(ad);
});
obj.list = lineArr;
polylineInit(obj, map, (v: any) => {
opd.push(v)
});
})
setPolylineList(opd)
}
const initMap = () => {
const { centerPoint } = initDataMap;
const center = [centerPoint.lng, centerPoint.lat];
const mzooms = [8, 19];
const mzoom = 12;
let map = new AMap.Map("AMapBox", {
zoom: mzoom, //初始化地图层级
zooms: mzooms,
rotateEnable: false, // 固定视角
disableSocket: true,
center: center,
});
mapRef.current = map;
addAreaCoordinate(map);
};
useEffect(() => {
initMap();
}, []);
// 地图道路线更新
useEffect(() => {
dealPolylineInit(roadMapData, mapRef.current);
}, [roadMapData]);
// 地图点更新
useEffect(() => {
if (isShowLanLat == 1) {
drawMarker(projectMapData, mapRef.current);
} else {
if (mapRef.current) {
mapRef.current.remove(markerList);// 清除markerList点位
}
}
}, [isShowLanLat, projectMapData]);
return (
<div>
<div id='AMapBox' style={{ width: '100%', height: 640 }}></div>
</div>
);
})
export default AMapModal
js高德地图添加点Marker,添加线段Polyline,添加一个区域Polygon(面)的更多相关文章
- Android集成高德地图如何自定义marker
高德地图自定义Marker 高德地图默认的marker样式是这种 一般的修改样式是通过icon接口来调整 MarkerOptions markerOptions = new MarkerOptions ...
- JS高德地图应用 ---- 鼠标点击加入标记 & POI搜索
代码如下 (填入Key值) : <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- JS框架_(JQuery.js)高德地图api
百度云盘 传送门 密码 :ko30 高德地图api效果 <!doctype html> <html> <head> <meta charset="u ...
- js高德地图手机定位
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- JS高德地图计算两地之间的实际距离
这个是通过导航的方式来获取两地之间的实际距离,和消耗的时间(key值自己去申请哈) <!doctype html> <html> <head> <meta c ...
- IOS原生地图与高德地图
原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 ...
- 如何实现在H5里调起高德地图APP?(上)
这一篇文章,将讲述如何在H5里调起高德地图APP,并展示兴趣点.适合于展示某个餐馆,商场等,让用户自行选择前往方式. 场景一.在高德地图上展示Marker点或者POI标记 在一些基于位置分享的应用开发 ...
- iOS之原生地图与高德地图
原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 2. ...
- iOS原生地图与高德地图的使用
原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 2. ...
- 如何实现在H5里调起高德地图APP?
http://www.cnblogs.com/milkmap/p/5912350.html 这一篇文章,将讲述如何在H5里调起高德地图APP,并展示兴趣点.适合于展示某个餐馆,商场等,让用户自行选择前 ...
随机推荐
- java中继承的内存分析
本文主要讲述java中继承的内存分析. 示例1,代码如下: public class EncapsulationTest { public static void main(String[] args ...
- 周而复始,往复循环,递归、尾递归算法与无限极层级结构的探究和使用(Golang1.18)
所有人都听过这样一个歌谣:从前有座山,山里有座庙,庙里有个和尚在讲故事:从前有座山....,虽然这个歌谣并没有一个递归边界条件跳出循环,但无疑地,这是递归算法最朴素的落地实现,本次我们使用Golang ...
- 7、解决swagger测试接口报错:TypeError: Failed to execute ‘fetch‘ on ‘Window‘: Request with GET/HEAD method cannot have body
一.Swagger报错: 1.报错类型: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method ...
- CH32V307以太网(芯片内部10M)
用过WCH的CH579M或者CH563的话,就会发现CH32V307这个自带的10M以太网代码编写与前两颗芯片流程十分相似.部分区别就在于初始化方面. 代码流程依旧按照 初始化--->等待PHY ...
- 体验一个前端视图层的mvvm的框架Knockoutjs(双向绑定,模板..)..解放您的双手,不再处理那么多的dom操作..快速实现视图层数据与UI的交互处理
笔者之前对于类似前端展示的,可能都是自己开发js对象,集合外加dom事件进行处理.. 近期看到相关资料,了解了Knockoutjs这个框架,下面来段代码: <script type=" ...
- Python Kconfiglib初次学习
1 参考 kconfiglib库官方介绍:kconfiglib · PyPI Kconfiglib源码:GitHub - ulfalizer/Kconfiglib: A flexible Python ...
- P4711 「化学」相对分子质量 代码
#include <bits/stdc++.h> using namespace std; double ret=0; namespace StringUtils { pair<st ...
- 解决xcode每次编译都需要输入用户名和密码
MacOS:11.1 Xcode:12.3 一.打开你的 钥匙串, 如果不知道 打开你的 spotlight搜索 工具 ,输入"钥匙串" 二.登录--->iPhone de ...
- 浅谈LCA问题(最近公共祖先)(四种做法)
[模板]最近公共祖先(LCA) \(update \ 2023.1.3\) 新增了树链剖分 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入格式 第一行包含三个正整数 \ ...
- Java 进阶P-11+P-12
文本流 在流上建立文本处理 PrintWriter pw = new PrintWriter()( new BufferedWriter( new Out put StreamWriter( new ...