我见过很多类似的问题(这里,这里和这里),但他们都接受了不能解决我的问题的答案。我发现这个问题的最佳解决方案是StyledMarker库,它允许您定义标记的自定义颜色,但我不能让它使用默认标记(当您做谷歌地图搜索时得到的-中间有一个点),它似乎只是提供了一个字母标记,或一个特殊的图标。
当前回答
你可以动态地请求图标图像从谷歌图表api与网址:
http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|FE7569
它看起来像这样:图像是21x34像素,针尖的位置是(10,34)
你还需要一个单独的阴影图像(这样它就不会重叠附近的图标):
http://chart.apis.google.com/chart?chst=d_map_pin_shadow
它看起来像这样:图像是40x37像素,针尖的位置(12,35)
当你构建你的MarkerImages时,你需要相应地设置大小和锚点:
var pinColor = "FE7569";
var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor,
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34));
var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
new google.maps.Size(40, 37),
new google.maps.Point(0, 0),
new google.maps.Point(12, 35));
然后你可以添加标记到你的地图:
var marker = new google.maps.Marker({
position: new google.maps.LatLng(0,0),
map: map,
icon: pinImage,
shadow: pinShadow
});
只需将“FE7569”替换为您需要的颜色代码。例如:
归功于Jack B Nimble的灵感;)
其他回答
我用StyledMarker所能得到的最接近的东西是这个。
不过,中间的子弹并不像默认的子弹那么大。StyledMarker类只是构建这个url,并要求谷歌api创建标记。
从类使用示例使用“%E2%80%A2”作为你的文本,如:
var styleMaker2 = new StyledMarker({styleIcon:new StyledIcon(StyledIconTypes.MARKER,{text:"%E2%80%A2"},styleIconClass),position:new google.maps.LatLng(37.263477473067, -121.880502070713),map:map});
你需要修改StyledMarker.js来注释掉这些行:
if (text_) {
text_ = text_.substr(0,2);
}
因为这将把文本字符串修剪为2个字符。
或者,你也可以创建自定义标记图像基于默认的一个你想要的颜色,并覆盖默认标记的代码,如:
marker = new google.maps.Marker({
map:map,
position: latlng,
icon: new google.maps.MarkerImage(
'http://www.gettyicons.com/free-icons/108/gis-gps/png/24/needle_left_yellow_2_24.png',
new google.maps.Size(24, 24),
new google.maps.Point(0, 0),
new google.maps.Point(0, 24)
)
});
下面是使用google Maps API本身的一个很好的解决方案。没有外部服务,没有额外的库。它可以自定义形状和多种颜色和样式。解决方案使用矢量标记,googlemaps api称之为符号。
除了少数且有限的预定义符号之外,您还可以通过指定SVG路径字符串(Spec)来制作任何颜色的任何形状。
要使用它,不要将“icon”标记选项设置为图像url,而是将其设置为包含符号选项的字典。例如,我设法制作了一个与标准标记非常相似的符号:
function pinSymbol(color) {
return {
path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z M -2,-30 a 2,2 0 1,1 4,0 2,2 0 1,1 -4,0',
fillColor: color,
fillOpacity: 1,
strokeColor: '#000',
strokeWeight: 2,
scale: 1,
};
}
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(latitude, longitude),
icon: pinSymbol("#FFF"),
});
如果你小心地保持形状关键点在0,0,你就避免了必须定义标记图标的定心参数。另一个路径示例,相同的标记没有点:
path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z',
这里有一面非常简单而丑陋的彩色旗帜:
path: 'M 0,0 -1,-2 V -43 H 1 V -2 z M 1,-40 H 30 V -20 H 1 z',
你也可以使用像Inkscape (GNU-GPL,多平台)这样的可视化工具来创建路径。一些有用的提示:
Google API just accepts a single path, so you have to turn any other object (square, cercle...) into a path and join them as a single one. Both commands at the Path menu. To move the path to the (0,0), go to the Path Edit mode (F2) select all the control nodes and drag them. Moving the object with F1, won't change the path node coords. To ensure the reference point is at (0,0), you can select it alone and edit the coords by hand on the top toolbar. After saving the SVG file, which is an XML, open it with an editor, look for the svg:path element and copy the content of the 'd' attribute.
我扩展了一些vokimon的答案,使其更方便地更改其他属性。
function customIcon (opts) {
return Object.assign({
path: 'M 0,0 C -2,-20 -10,-22 -10,-30 A 10,10 0 1,1 10,-30 C 10,-22 2,-20 0,0 z M -2,-30 a 2,2 0 1,1 4,0 2,2 0 1,1 -4,0',
fillColor: '#34495e',
fillOpacity: 1,
strokeColor: '#000',
strokeWeight: 2,
scale: 1,
}, opts);
}
用法:
marker.setIcon(customIcon({
fillColor: '#fff',
strokeColor: '#000'
}));
或者在定义一个新标记时:
const marker = new google.maps.Marker({
position: {
lat: ...,
lng: ...
},
icon: customIcon({
fillColor: '#2ecc71'
}),
map: map
});
有时候很简单的事情,也可以用复杂来回答。我并不是说上面的答案都是不正确的,但我只是想说,它可以这么简单:
我知道这个问题很老了,但是如果有人只是想改变大头针或标记颜色,那么请查看文档:https://developers.google.com/maps/documentation/android-sdk/marker
当你添加你的标记时,只需设置图标属性:
GoogleMap gMap;
LatLng latLng;
....
// write your code...
....
gMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
有10种默认颜色可供选择。如果这还不够(简单的解决方案),那么我可能会选择其他答案中给出的更复杂的解决方案,满足更复杂的需求。
ps:我在另一个答案中写了一些类似的东西,因此我应该参考那个答案,但上次我这么做的时候,我被要求张贴答案,因为它太短了(就像这个)。
你可以动态地请求图标图像从谷歌图表api与网址:
http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|FE7569
它看起来像这样:图像是21x34像素,针尖的位置是(10,34)
你还需要一个单独的阴影图像(这样它就不会重叠附近的图标):
http://chart.apis.google.com/chart?chst=d_map_pin_shadow
它看起来像这样:图像是40x37像素,针尖的位置(12,35)
当你构建你的MarkerImages时,你需要相应地设置大小和锚点:
var pinColor = "FE7569";
var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor,
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34));
var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
new google.maps.Size(40, 37),
new google.maps.Point(0, 0),
new google.maps.Point(12, 35));
然后你可以添加标记到你的地图:
var marker = new google.maps.Marker({
position: new google.maps.LatLng(0,0),
map: map,
icon: pinImage,
shadow: pinShadow
});
只需将“FE7569”替换为您需要的颜色代码。例如:
归功于Jack B Nimble的灵感;)