javascript 谷歌地图 API v3 设置标记并获得点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7083264/
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
google maps API v3 set marker and get point
提问by user821738
I have a problem to set marker and get latitude and longitude for that marker. How can I do that in google maps v3 API
我在设置标记并获取该标记的纬度和经度时遇到问题。我怎么能在谷歌地图 v3 API 中做到这一点
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("divGoogleMaps"), myOptions);
google.maps.event.addListener(map, 'click', function() {
});
this is my start code.
这是我的开始代码。
回答by Nicolas
You should check Google Maps API doc web-site they have a fex example to help you started.
您应该检查 Google Maps API doc 网站,他们有一个 fex 示例来帮助您入门。
http://code.google.com/apis/maps/documentation/javascript/basics.html
http://code.google.com/apis/maps/documentation/javascript/basics.html
google.maps.event.addListener(map, 'click', function(e) {
placeMarker(e.latLng, map);
});
}
function placeMarker(position, map) {
var marker = new google.maps.Marker({
position: position,
map: map
});
map.panTo(position);
}
Here you set a marker and get the position.
在这里你设置一个标记并获得位置。
回答by idrumgood
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
Taken directly from Google Maps API v3
If you want to attach an event listener to the marker, it is advisable to do it right after you add it to the map, while you still have a fresh reference to it (in the case where you're potentially looping through some marker adding code, as is common).
如果您想将事件侦听器附加到标记,建议您在将其添加到地图后立即执行此操作,同时您仍然对它有一个新的引用(在您可能会循环添加某些标记的情况下)代码,这是常见的)。