谷歌地图API,无法获得一个地方的名称

时间:2016-04-24 10:19:20

标签: javascript google-maps google-places-api

我正在尝试使用Google Map API做一个小项目,我的问题是当我点击它时,我找不到如何在地图上获取地点的名称。按地方我的意思是表示为内部有图片的圆圈(博物馆,餐馆,纪念碑等) Like this

在这张图片中,地图的默认动作是当我点击它时打开一个信息窗口,带有地点的名称,这正是我想要的但是我没有“提取”名称的解决方案(在其他地方使用它)因为这是一个默认的反应......

我尝试使用以下代码获取它:

var service = new google.maps.places.PlacesService(map);

google.maps.event.addListener(map, 'click', function(event) {
    getAddress(event.latLng);
});

function getAddress(latLng) {
    geocoder.geocode( {'latLng': latLng},
    function(results, status) {
        if(status === google.maps.GeocoderStatus.OK) {
            var arrayLength = results.length;

            for (var i = 0; i < arrayLength; i++) {
                var request = {
                    placeId: results[i].place_id_id
                };
                service.getDetails(request, callback);
            }
        }
    });
}

function callback(place, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        console.log(place);
    }
}

但唯一准备好的东西是地址(例如:“37 Phillip St,Sydney NSW 2000,Australie”),但从来没有这个地方的名字。

我在谷歌上找不到任何东西,看起来真的很奇怪......

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:1)

As per the code you have provided, you are using the PlaceDetails from which you can actually already get the data you need. As per the Places Details Results docs:

A successful getDetails() call returns a PlaceResult object.

Wherein PlaceResult object pretty much has the info that you might need. Haven't tested it out, but it has the address_components property that has a structure like so (referred to Geocoder response sample on this one):

"address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ]

and also the name property which is simply described in the docs as:

The place's name.

Just tweak around the results of the PlaceDetails request and I'm pretty sure you'll find it. Hope this helps. Good luck.