javascript 以正确的方式更改 Google Maps V3 Maker 图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7965288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Google Maps V3 Maker Icon the correct way?
提问by Niklas
The code in example 1 works. I would like to understand the marker.setIcon(); function more (new to JavaScript also).
示例 1 中的代码有效。我想了解marker.setIcon(); 功能更多(也是 JavaScript 新手)。
My question is. In the documentation for Google maps you see something like this for changing the marker.
我的问题是。在 Google 地图的文档中,您会看到类似更改标记的内容。
MarkerImage(url:string, size?:Size, origin?:Point, anchor?:Point, scaledSize?:Size)
How does this relate to what I have done in example 1 for setting up marker Icon, shoud I have done somethign like this instead?
这与我在示例 1 中为设置标记图标所做的事情有什么关系,我应该这样做吗?
marker = google.maps.MarkerImage({
url: "newIcon.png"
});
marker.setIcon(marker);
and would that have worked?
那会奏效吗?
here is my example
这是我的例子
Example 1
示例 1
function initialize(){
//MAP
var latlng = new google.maps.LatLng('xxx','xxx');
var options = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
//GEOCODER
geocoder = new google.maps.Geocoder();
marker = new google.maps.Marker({
map: map,
draggable: true
});
marker.setPosition(latlng);
marker.setIcon("newIcon.png");
map.setCenter(latlng);
}
回答by danski
You're giving a V2 answer for a V3 question.
您正在为 V3 问题提供 V2 答案。
There is no GIcon in V3.
V3 中没有 GIcon。
var image = new google.maps.MarkerImage("newIcon.png");
Can be used inside your marker as the icon.
可以在您的标记内用作图标。
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lng),
icon:image
});