gmaps v3从event.latLng获取纬度 [英] gmaps v3 get latitude from event.latLng
问题描述
我如何从event.latLng去除经度和纬度?
它返回一个 point()
,但我只需要lat和long。
我无法得到这个工作。根据 解决方案
html#MouseEventrel =noreferrer> MouseEvents
, event.latLng
的API包含一个 LatLng
,而不是 点
。如果是这种情况,那么您可以使用 lat()
和 lng()
方法分别获取值。如果 event.latLng
实际上是一个 Point
,那么您可以使用 x直接访问坐标和
y
属性(不是方法)。
什么类型的侦听器正在创建事件?
编辑:有一个教程中的示例,介绍如何做你想做的事。它看起来像你正在关注这一点。您是否记得包含实际的 placeMarker()
函数声明?
$ b
函数placeMarker(location){
var clickedLocation = new google.maps.LatLng(location);
var marker = new google.maps.Marker({
position:location,
map:map
});
map.setCenter(location);
}
或者您对放置标记不感兴趣,只想获得lat和lng值?在这种情况下,所有你需要的是:
google.maps.event.addListener(map, 'click',function(event){
var myLatLng = event.latLng;
var lat = myLatLng.lat();
var lng = myLatLng.lng();
})
I have an old gmaps application using V2 and I am trying to update it to v3.
I have a really simple problem, but I can't find a solution yet.
How can I strip the latitude and the longitude from the "event.latLng"?
It is returning a point()
, but I need only the lat alone and the long for itself.
I cant get this to work.
According to the API for MouseEvents
, event.latLng
contains a LatLng
, not a Point
. If this is the case then you can use the lat()
and lng()
methods to get the values separately. If event.latLng
is actually a Point
then you can directly access the coordinates using the x
and y
properties (not methods).
What type of listener is creating the event?
Edit: there's an example in the tutorial of how to do what you want. It looks like you're following this already. Did you remember to include the actual placeMarker()
function declaration?
function placeMarker(location) {
var clickedLocation = new google.maps.LatLng(location);
var marker = new google.maps.Marker({
position: location,
map: map
});
map.setCenter(location);
}
Or are you not interested in placing a marker, and just want to get the lat and lng values? In that case, all you need is:
google.maps.event.addListener(map, 'click', function(event) {
var myLatLng = event.latLng;
var lat = myLatLng.lat();
var lng = myLatLng.lng();
})
这篇关于gmaps v3从event.latLng获取纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!