这是一个简单的 map ,根据城市名称进行聚焦,您可以放置标记来指示您居住的地方。这个想法是将经度和纬度保存在数据库中,但首先我需要获取它。
到目前为止我有代码:
var myLatlng = new google.maps.LatLng(37.775, -122.4183333);
var myOptions =
{
zoom: 3,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var pais = 'Argentina';
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': pais}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.fitBounds(results[0].geometry.bounds);
} else {
alert("Geocode was not successful for the following reason: " + status);
};
var infoWindow = new google.maps.InfoWindow({
content: "<div style='font-size: 8pt; font-family: verdana ;' >Mi marca situada en<br></div>"
});
var marker;
function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
} else {
var clickedLocation = new google.maps.LatLng(location);
marker = new google.maps.Marker({
position: location,
map: map,
title: 'hola',
});
}
}
google.maps.event.addListener(map, 'click', function(event) {
infoWindow.open(map,marker);
placeMarker(event.latLng);
});
});
我尝试过各种方法,但都没有得到好的结果。我想用 gmaps api v3 来做。 这里JSFiddle .
最佳答案
拉杜所说的,主要是。我是updating your fiddle同时他似乎也是。此外,您可能会发现在构建 infoWindow 对象之前等待获得 latLng 会更方便,如下所示:
var infoWindow = new google.maps.InfoWindow({
content: "<div class='infoWindow'>Mi marca situada en<br>"
+ "Lat: " + event.latLng.lat() + "<br>"
+ "Lng: " + event.latLng.lng() + "</div>"
});
infoWindow.open(map, marker);
如果您想更新 infoWindow 的内容,请使用 infoWindow.setContent()
。
请参阅Google Maps API reference获取对象及其属性的详尽列表。
关于javascript - 我需要获取标记的纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26876806/