高德地图内置了许多遮盖物的api,今天主要就是讲解常用的点,线,面的绘制方法
点的绘制相对简单,只需要一个坐标值即可,例: [ 116.396783,39.894813 ]
创建地图
为了更好的展示点的绘制,我们需要先创建一个地图对象
<body>
<div id="container"></div>
</body>
const map = window.map = new AMap.Map("container", {
center: [116.381674, 39.910732],
viewMode: "3D",
zoom: 8,
});
点的类型
高德地图提供了多种点的绘制类型,其中包括图标Marker,文字text,灵活点标记ElasticMarker,标注类LabelMarker 等
Marker类
new AMap.Marker({
map,
position: [115.887289, 39.961275],
icon: "https://cloud-change.oss-cn-beijing.aliyuncs.com/icon/4.png?versionId=CAEQGxiCgICm78D17RciIDE1NWRhYjZlZmQyMDQyZjZiZGViOGEzNDFkYmQwNGQ5",
});
地图:
Marker的四个主要属性
- map:要显示该marker的地图对象
- position:点标记在地图上显示的位置,默认为地图中心点
- icon:需在点标记中显示的图标。可以是一个本地图标地址,或者Icon对象。有合法的content内容时,此属性无效
- content:点标记显示内容,可以是HTML要素字符串或者HTML DOM对象。content有效时,icon属性将被覆盖
icon和content 任选其一
当然 marker的属性 还有很多
Text类
new AMap.Text({
map,
position: [115.618336, 39.87965],
text: "我是text",
});
text 与marker属性相似,不同就在于icon换成了text。
Text属性,请点我
ElasticMarker
灵活点标记,一种可以随着地图级别变化而改变图标和大小的点标记,插件类
对于插件类,在使用时需要额外引入plugin=ElasticMarker ,稍后可以在文章尾部的全部代码中查看如何引入
new AMap.ElasticMarker({
map: map,
position: [115.933104, 39.998766],
styles: [
{
icon: {
img: "https://a.amap.com/jsapi_demos/static/resource/img/trees.png",
size: [366, 201],
ancher: [183, 101],
imageSize: [865, 1156],
imageOffset: [45, 480],
fitZoom: 17.5,
scaleFactor: 2,
maxScale: 2,
minScale: 0.125,
},
},
],
zoomStyleMapping: {
8: 0,
9: 0,
10: 0,
11: 0,
12: 0,
13: 0,
14: 0,
15: 0,
16: 0,
17: 0,
18: 0,
19: 0,
20: 0,
},
});
ElasticMarker主要四个属性
- map:要显示该marker的地图对象
- position:点标记在地图上显示的位置,默认为地图中心点
- styles:这是个对象数组,主要设置点的样式,具体采用哪个样式由zoomStyleMapping决定
- zoomStyleMapping:是个对象,如上面的例子中 的 14 :0 ,表示当地图的缩放等级为14级时,采用styles中下标为0的样式
LabelMarker 类
let layer = new AMap.LabelsLayer({
zooms: [3, 20],
zIndex: 1000,
// 开启标注避让,默认为开启,v1.4.15 新增属性
collision: true,
// 开启标注淡入动画,默认为开启,v1.4.15 新增属性
animation: true,
});
map.add(layer);
let labelMarker = new AMap.LabelMarker({
name: "京味斋烤鸭店",
position: [115.898359,39.909869 ],
icon: {
type: "image",
image: "https://a.amap.com/jsapi_demos/static/images/poi-marker.png",
clipOrigin: [547, 92],
clipSize: [50, 68],
size: [25, 34],
anchor: "bottom-center",
angel: 0,
retina: true,
},
text: {
content: "京味斋烤鸭店",
direction: "top",
offset: [0, 0],
style: {
fontSize: 13,
fontWeight: "normal",
fillColor: "#fff",
padding: "2, 5",
backgroundColor: "#22884f",
},
},
});
layer.add(labelMarker);
LabelMarker 需要依赖于LabelsLayer ,labelMarker 可以同时设置图标和文字
其他属性
到此 点的绘制就基本介绍完了,对于这些点对象他们有一些共同且的方法和事件都是日常开发中非常常见的
let text = new AMap.Text({
map,
position: [115.618336, 39.87965],
text: "我是text",
});
//隐藏
text.hide()
//显示
text.show()
text.on('click',function(e){
console.log(e)
})
全部代码展示 (其中xxxx 高德的秘钥,cccc是高德的key,需要自己申请)
<!DOCTYPE html>
<html>
<head>
<meta
name="viewport"
content="width=device-width initial-scale=1.0 maximum-scale=1.0 user-scalable=0"
/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>map</title>
<style>
body,
html,
#container {
margin: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="container"></div>
</body>
<script>
window._AMapSecurityConfig = {
securityJsCode: "xxxx",
};
</script>
<script
language="javascript"
src="https://webapi.amap.com/maps?v=1.4.15&key=cccc&plugin=ElasticMarker"
></script>
<script language="javascript">
const map = (window.map = new AMap.Map("container", {
center: [116.381674, 39.910732],
viewMode: "3D",
zoom: 8,
showLabel: false,
}));
//--------------------------------------Marker--------------------------------------
new AMap.Marker({
map,
position: [115.887289, 39.961275],
icon: "https://cloud-change.oss-cn-beijing.aliyuncs.com/icon/4.png?versionId=CAEQGxiCgICm78D17RciIDE1NWRhYjZlZmQyMDQyZjZiZGViOGEzNDFkYmQwNGQ5",
});
//--------------------------------------Text--------------------------------------
let text = new AMap.Text({
map,
position: [115.618336, 39.87965],
text: "我是text",
});
//隐藏
text.hide()
//显示
text.show()
text.on('click',function(e){
console.log(e)
})
//--------------------------------------ElasticMarker--------------------------------------
new AMap.ElasticMarker({
map: map,
position: [115.933104, 39.998766],
styles: [
{
icon: {
img: "https://a.amap.com/jsapi_demos/static/resource/img/trees.png",
size: [366, 201],
ancher: [183, 101],
imageSize: [865, 1156],
imageOffset: [45, 480],
fitZoom: 17.5,
scaleFactor: 2,
maxScale: 2,
minScale: 0.125,
},
},
],
zoomStyleMapping: {
8: 0,
9: 0,
10: 0,
11: 0,
12: 0,
13: 0,
14: 0,
15: 0,
16: 0,
17: 0,
18: 0,
19: 0,
20: 0,
},
});
//--------------------------------------LabelMarker--------------------------------------
var layer = new AMap.LabelsLayer({
zooms: [3, 20],
zIndex: 1000,
// 开启标注避让,默认为开启,v1.4.15 新增属性
collision: true,
// 开启标注淡入动画,默认为开启,v1.4.15 新增属性
animation: true,
});
map.add(layer);
var labelMarker = new AMap.LabelMarker({
name: "京味斋烤鸭店",
position: [115.898359,39.909869 ],
icon: {
type: "image",
image: "https://a.amap.com/jsapi_demos/static/images/poi-marker.png",
clipOrigin: [547, 92],
clipSize: [50, 68],
size: [25, 34],
anchor: "bottom-center",
angel: 0,
retina: true,
},
text: {
content: "京味斋烤鸭店",
direction: "top",
offset: [0, 0],
style: {
fontSize: 13,
fontWeight: "normal",
fillColor: "#fff",
padding: "2, 5",
backgroundColor: "#22884f",
},
},
});
layer.add(labelMarker);
</script>
</html>