Elastic Marker
This article introduces how to use the AMap.ElasticMarker plugin to create elastic markers, which can change icon styles based on the map's zoom level, thereby providing a richer and more personalized display effect when users zoom the map.
Elastic Marker Example
1、Add Elastic Marker
1.1 Create Map
var map = new AMap.Map('container', {
zoom: 10,
showOversea: true, //Enable world Map
center: [77.19, 28.59]
});1.2 Create Style List
var stylesArray = [
{
icon: { //Icon Style
img: "https://a.amap.com/jsapi_demos/static/resource/img/men3.png",
size: [16, 16], //Original Size of Icon
anchor: "bottom-center", //Anchor Position
fitZoom: 14, //The most suitable level to display the icon in its original size
scaleFactor: 2, //Scaling factor for one level of map zoom
maxScale: 2, //The maximum zoom in scale of the image, as the map zooms in, the icon will zoom in accordingly, with a maximum of 2
minScale: 1, //The minimum zoom out scale of the image, as the map zooms out, the icon will zoom out accordingly, with a minimum of 1
},
label: { //Text Annotation
content: "Hundred Flowers Hall", //Text content
position: "BM", //The reference point of the text position relative to the icon, \"BM\" is the bottom center
minZoom: 15, //The minimum display level of the label, that is, the text annotation will only be displayed at map level 15 and above
},
},
{
icon: {
img: "https://a.amap.com/jsapi_demos/static/resource/img/tingzi.png",
size: [48, 63],
anchor: "bottom-center",
fitZoom: 17.5,
scaleFactor: 2,
maxScale: 2,
minScale: 0.125,
},
label: {
content: "Longevity Pavilion",
position: "BM",
minZoom: 15,
},
},
];Each object in the stylesArray represents the style settings of a point marker. For example, the first object represents the landmark style of "Hundred Flowers Hall", and the second object represents the landmark style of "Longevity Pavilion".
1.3 Create level mapping for style list
var zoomStyleMapping = {
14: 0, //Levels 14-17 use style 0
15: 0,
16: 0,
17: 0,
18: 1, //Levels 18-20 use style 1
19: 1,
20: 1,
};zoomStyleMapping defines the display of different point marker styles at the corresponding zoom levels of the map. When the zoom level is 14-17, the 0th style element in the style array (the landmark style of "百花殿" in this example) will be used. When the zoom level is 18-20, the 1st style element (the landmark style of "万寿亭" in this example) will be used.
1.4 Load the flexible point marker plugin
It is recommended to use asynchronous installation plugins, the method of plugin introduction and plugin usage.
AMap.plugin(["AMap.ElasticMarker"], function () {
var elasticMarker = new AMap.ElasticMarker({
position: [77.19, 28.59],
styles: stylesArray, //Specify the style list
zoomStyleMapping: zoomStyleMapping, //Specify the mapping of zoom and style
});
map.add(elasticMarker);
map.setFitView(); //Zoom the map to the appropriate level of view
});