当两个标记彼此靠近时,标记标签重叠。如何为 google 地图标记提供背景颜色?
Marker label overlapping when two markers are close to each other. How to give background color to google maps marker?
我正在使用 google 地图版本 3 api 和 google 地图标记。当标记彼此靠近时,标签重叠并且看起来很糟糕。我试图为标记标签提供背景颜色,但 google 地图标记标签似乎没有背景颜色 属性。我尝试使用 MarkerWithLabel api,但与 google 地图默认标记标签相比,当我有 1000 个标记时渲染速度较慢。我已经包含了 js fiddle。谁能帮我解决这个问题?
https://jsfiddle.net/dcvc99qz/12/
function initMap() {
var pointA = new google.maps.LatLng(51.2750, 1.0870),
pointB = new google.maps.LatLng(51.2750, 0.8140),
myOptions = {
zoom: 5,
center: pointA,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "AA",
map: map
}),
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
map: map
});
}
initMap();
标记只是图像,你不能只改变它的背景颜色,你必须定义一个新的图标。首先,准备新的颜色正确的透明图像。然后创建新的图标定义。
var icons = {
green: {
// link to an image, use https if original site also uses https
url: 'https://i.imgur.com/5Yd25Ga.png',
// set size of an image
size: new google.maps.Size(22, 40),
// if an image is a sprite, set its relative position from 0, 0
origin: new google.maps.Point(0, 0),
// part of an image pointing on location on the map
anchor: new google.maps.Point(11, 40),
// position of text label on icon
labelOrigin: new google.maps.Point(11, 10)
},
red: {
// ...
}
};
的文档
添加新标记时,将其图标设置为定义的图标之一。
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
icon: icons.green,
map: map
});
当点彼此靠近时,您会注意到文本标签重叠。要修复它,请向每个标记添加递增 zIndex
。
markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "AA",
zIndex: 1000,
icon: icons.red,
map: map
});
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
zIndex: 1001,
icon: icons.green,
map: map
});
完整的工作示例:jsfiddle.net/yLhbxnz6/
我正在使用 google 地图版本 3 api 和 google 地图标记。当标记彼此靠近时,标签重叠并且看起来很糟糕。我试图为标记标签提供背景颜色,但 google 地图标记标签似乎没有背景颜色 属性。我尝试使用 MarkerWithLabel api,但与 google 地图默认标记标签相比,当我有 1000 个标记时渲染速度较慢。我已经包含了 js fiddle。谁能帮我解决这个问题?
https://jsfiddle.net/dcvc99qz/12/
function initMap() {
var pointA = new google.maps.LatLng(51.2750, 1.0870),
pointB = new google.maps.LatLng(51.2750, 0.8140),
myOptions = {
zoom: 5,
center: pointA,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "AA",
map: map
}),
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
map: map
});
}
initMap();
标记只是图像,你不能只改变它的背景颜色,你必须定义一个新的图标。首先,准备新的颜色正确的透明图像。然后创建新的图标定义。
var icons = {
green: {
// link to an image, use https if original site also uses https
url: 'https://i.imgur.com/5Yd25Ga.png',
// set size of an image
size: new google.maps.Size(22, 40),
// if an image is a sprite, set its relative position from 0, 0
origin: new google.maps.Point(0, 0),
// part of an image pointing on location on the map
anchor: new google.maps.Point(11, 40),
// position of text label on icon
labelOrigin: new google.maps.Point(11, 10)
},
red: {
// ...
}
};
的文档
添加新标记时,将其图标设置为定义的图标之一。
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
icon: icons.green,
map: map
});
当点彼此靠近时,您会注意到文本标签重叠。要修复它,请向每个标记添加递增 zIndex
。
markerA = new google.maps.Marker({
position: pointA,
title: "point A",
label: "AA",
zIndex: 1000,
icon: icons.red,
map: map
});
markerB = new google.maps.Marker({
position: pointB,
title: "point B",
label: "BB",
zIndex: 1001,
icon: icons.green,
map: map
});
完整的工作示例:jsfiddle.net/yLhbxnz6/