Javascript 在用户单击的位置显示lat lng
我想显示纬度和经度,用户可以在地图上单击任意位置(我没有使用marker event),但它仍然显示。这是我的密码:Javascript 在用户单击的位置显示lat lng,javascript,google-maps,web,Javascript,Google Maps,Web,我想显示纬度和经度,用户可以在地图上单击任意位置(我没有使用marker event),但它仍然显示。这是我的密码: var myCenter=new google.maps.LatLng(51.508742,-0.120850); function initialize() { var mapProp = { center:myCenter, zoom:5, mapTypeId:google.maps.MapTypeId.ROAD
var myCenter=new google.maps.LatLng(51.508742,-0.120850);
function initialize()
{
var mapProp = {
center:myCenter,
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
var marker=new google.maps.Marker({
position:myCenter,
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(map, 'click', function(event)
{
infowindow.setContent(event.LatLng.lat()+","+event.latlng.lng());
infowindow.open(map,marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
有人能帮忙吗?试试这个,当用户点击地图时,它会告诉你如何获得经度和纬度
google.maps.event.addListener(map, 'click', function( event ){
alert( "Latitude: "+event.latLng.lat()+" "+", longitude: "+event.latLng.lng() );
});
更新标记的
位置
-属性以及单击
-处理程序:
google.maps.event.addListener(map, 'click', function(event)
{
marker.setPosition(event.latLng);
infowindow.setContent(event.latLng.lat()+","+event.latLng.lng());
infowindow.open(map,marker);
});
注意:它必须完全是latLng
,而不是latLng
或latLng
演示:
函数初始化(){
var myCenter=newgoogle.maps.LatLng(51.508742,-0.120850);
var mapProp={
中心:迈森特,
缩放:5,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“googleMap”),mapProp);
var marker=new google.maps.marker({
职位:迈森特,
});
marker.setMap(map);
var infowindow=new google.maps.infowindow();
google.maps.event.addListener(映射,'click',函数(事件){
标记器设置位置(事件锁定);
setContent(event.latLng.lat()+”,“+event.latLng.lng());
信息窗口。打开(地图、标记);
});
google.maps.event.trigger(map,'click',{latLng:map.getCenter()})
}
google.maps.event.addDomListener(窗口“加载”,初始化)代码>
html,
身体,
#谷歌地图{
身高:100%;
保证金:0;
填充:0;
}
hey@laurence,这很有效。很好。但我想使用信息窗口。但是,如果我不能解决我的问题,我将使用此代码。非常感谢:)如果你觉得这有用,请接受它作为答案。欢迎您,祝您愉快。:)当你点击上面的“运行代码片段”时,它不起作用了?哦,是的,它起作用了。这就是我想要的。非常感谢:)