javascript 向 GMaps(谷歌地图)添加标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15907626/
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
Adding a marker to GMaps (google maps)
提问by user1738017
I'm trying to use this tool: http://software.stadtwerk.org/google_maps_colorizr/to add colour to my google map, here is my example:
我正在尝试使用此工具:http: //software.stadtwerk.org/google_maps_colorizr/为我的谷歌地图添加颜色,这是我的示例:
http://jsfiddle.net/xUUxn/851/
http://jsfiddle.net/xUUxn/851/
The colour works fine, but now I can't seem to add any markers to it, I've tried putting this example code in:
颜色工作正常,但现在我似乎无法为其添加任何标记,我尝试将此示例代码放入:
map.addMarker({
lat: -12.043333,
lng: -77.028333,
title: 'Lima',
infoWindow: {
content: '<p>HTML Content</p>'
}
});
But it doesn't seem to work, I'm not entirely sure where to put it or even if the references are correct.
但它似乎不起作用,我不完全确定把它放在哪里,或者即使引用是正确的。
回答by lucuma
In order to create a marker you need to do the following:
要创建标记,您需要执行以下操作:
Demo forked from your example: http://jsfiddle.net/lucuma/xUUxn/852/
演示从您的示例中分叉:http: //jsfiddle.net/lucuma/xUUxn/852/
JS:
JS:
var marker = new google.maps.Marker({
position: new google.maps.LatLng( -12.043333,-77.028333),
map: map,
title: 'Hello World!'
});
To add an info window overlay:
添加信息窗口覆盖:
var contentString = '<div id="content"><h1>Overlay</h1></div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
回答by dersvenhesse
The documentation says:
文档说:
var marker = new google.maps.Marker({
position: new google.maps.LatLng(0,0),
map: map,
title:"Hello World!"
});
In your Fiddle: http://jsfiddle.net/xUUxn/854/
在你的小提琴中:http: //jsfiddle.net/xUUxn/854/
回答by user3235591
I was able to use a loop, by this using 'this' as second parameters to to infoWindow.open()
我能够使用循环,通过使用“this”作为 infoWindow.open() 的第二个参数
google.maps.event.addListener(marker, 'click', function() {
infowindow = new ....;
infowindow.open(map,this);
});