Javascript PlaceResult 对象将纬度/经度作为对象返回,不确定如何单独获取它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10647389/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:20:19  来源:igfitidea点击:

PlaceResult object returns latitude/longitude as object, not sure how to get them individually

javascriptgoogle-mapsgoogle-places-api

提问by dallen

I'm testing out the Google Places autocomplete feature here:

我正在此处测试 Google 地方信息自动完成功能:

https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

I want to get the latitude and longitude of the Place, but I'm having some troubles. When I use the following code:

我想获取 Place 的纬度和经度,但遇到了一些麻烦。当我使用以下代码时:

var place = autocomplete.getPlace();
console.log(place.geometry.location);

I get this returned:

我得到这个返回:

enter image description here

在此处输入图片说明

When I use it in an infoWindow like this:

当我在这样的 infoWindow 中使用它时:

infowindow.setContent('
<div><strong>' + place.name + '</strong><br>' + place.geometry.location);

place.geometry.locationis then displayed like this:

place.geometry.location然后显示如下:

(53.539834, -113.49402099999998)

(53.539834, -113.49402099999998)

All I want to do is get the lat and lng separately. place.geometry.location.latdoesn't work. Not sure what else to do.

我想要做的就是分别获取 lat 和 lng。place.geometry.location.lat不起作用。不知道还能做什么。

回答by JDev

You can use like this.

你可以这样使用。

var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();  

回答by Sabbir

You can use:

您可以使用:

var LatLng = place.geometry.location.toJSON();

回答by Muthu

It's working fine for me.

它对我来说很好用。

marker.setIcon(image);

marker.setPosition(place.geometry.location);

var address = '';
if (place.address_components) {
    address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
    ].join(' ');
}
alert(place.geometry.location.lat());
alert(place.geometry.location.lng());
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);

回答by Sushil

You can get the LATand LONGfrom the PLACESobject.

您可以从PLACES对象获取LATLONG

Like this :

像这样 :

const autocomplete = new google.maps.places.Autocomplete(this.inputNativeElement.nativeElement as HTMLInputElement);

autocomplete.addListener('place_changed', () => {
    const place = autocomplete.getPlace();

    let cus_location = {
        lat: place.geometry.location.lat(),
        long: place.geometry.location.lng()
    }

}