谷歌地图自动填充:如何获得lat / lng?

时间:2022-05-05 00:53:32

I'm using Google Maps' autocomplete feature for geocoding. It seems to return a lot of data, but not the lat/lng. Is there a way to get this information? I'd like to pass it back to my backend application for further processing.

我正在使用Google地图的自动填充功能进行地理编码。它似乎返回了大量数据,但不是lat / lng。有没有办法获得这些信息?我想将它传递回我的后端应用程序以进行进一步处理。

2 个解决方案

#1


23  

OK, I'm not familiar with Places or Autocomplete. But it looks like what you are looking for is

好的,我不熟悉Places或Autocomplete。但看起来你正在寻找的是

autocomplete.getPlace().geometry.location

To demonstrate I took this example and added the line above to create this JSFiddle where you can enter the place name and when it's selected, an infowindow with the LatLng is created.

为了演示我采用这个例子并添加上面的行来创建这个JSFiddle,你可以在其中输入地名,当它被选中时,创建一个带有LatLng的infowindow。

In particular it listens to the user's selection, then refreshes the infowindow.

特别是它会监听用户的选择,然后刷新信息窗口。

google.maps.event.addListener(autocomplete, 'place_changed', function() {
      infowindow.close();
      var place = autocomplete.getPlace();
       ...
      infowindow.setContent('<div><strong>' + place.name + 
        '</strong><br>' + address + "<br>" + place.geometry.location);

#2


39  

There are methods called lat() and lng() that exist on the geometry object's prototype. So to get the values, just use the following:

在几何对象的原型中存在名为lat()和lng()的方法。因此,要获取值,只需使用以下内容:

var place = autocomplete.getPlace();

var lat = place.geometry.location.lat(),
    lng = place.geometry.location.lng();

// Then do whatever you want with them

console.log(lat);
console.log(lng);

console.warn('Warning: I didn\'t test this code!');

#1


23  

OK, I'm not familiar with Places or Autocomplete. But it looks like what you are looking for is

好的,我不熟悉Places或Autocomplete。但看起来你正在寻找的是

autocomplete.getPlace().geometry.location

To demonstrate I took this example and added the line above to create this JSFiddle where you can enter the place name and when it's selected, an infowindow with the LatLng is created.

为了演示我采用这个例子并添加上面的行来创建这个JSFiddle,你可以在其中输入地名,当它被选中时,创建一个带有LatLng的infowindow。

In particular it listens to the user's selection, then refreshes the infowindow.

特别是它会监听用户的选择,然后刷新信息窗口。

google.maps.event.addListener(autocomplete, 'place_changed', function() {
      infowindow.close();
      var place = autocomplete.getPlace();
       ...
      infowindow.setContent('<div><strong>' + place.name + 
        '</strong><br>' + address + "<br>" + place.geometry.location);

#2


39  

There are methods called lat() and lng() that exist on the geometry object's prototype. So to get the values, just use the following:

在几何对象的原型中存在名为lat()和lng()的方法。因此,要获取值,只需使用以下内容:

var place = autocomplete.getPlace();

var lat = place.geometry.location.lat(),
    lng = place.geometry.location.lng();

// Then do whatever you want with them

console.log(lat);
console.log(lng);

console.warn('Warning: I didn\'t test this code!');