简体   繁体   English

Google Places API getDetails formatted_phone_number返回undefined

[英]Google Places API getDetails formatted_phone_number returns undefined

I have created this bin to get the details of a place - formatted address and formatted phone number. 我已创建此bin以获取地点的详细信息 - 格式化的地址和格式化的电话号码。

However, the formatted Phone number shows undefined for the places. 但是,格式化的电话号码显示未定义的地点。

However it does show up phone number for places like "Franklin D. Roosevelt Four Freedoms Park, 1 FDR Four Freedoms Park, Roosevelt Island, NY 10044, USA" 然而它确实出现了像“Franklin D. Roosevelt Four Freedoms Park,1 FDR Four Freedoms Park,Roosevelt Island,NY 10044,USA”这样的地方的电话号码。

But, most of the other places are returning undefined. 但是,大多数其他地方都未定义。

For example this place, https://www.google.co.in/maps/place/iStorage+Limited/@51.533075,-0.3119974,17z/data=!3m1!4b1!4m5!3m4!1s0x4876126b1e57f67d:0x4c6d4388c42b1f06!8m2!3d51.5330717!4d-0.3098087 shows phone number in the map. 例如,这个地方, https://www.google.co.in/maps/place/iStorage+Limited/@51.533075,-0.3119974,17z/data=!3m1!4b1!4m5!3m4!1s0x4876126b1e57f67d:0x4c6d4388c42b1f06!8m2! 3d51.5330717!4d-0.3098087在地图中显示电话号码。

But, searching for in the jsbin page, "iStorage Limited 13 Alperton Ln, Perivale, Greenford, Middlesex UB6 8DH, UK" does show that the phone number is undefined. 但是,在jsbin页面中搜索“iStorage Limited 13 Alperton Ln,Perivale,Greenford,Middlesex UB6 8DH,UK”确实显示电话号码未定义。

Am I doing something wrong here in the code ? 我在代码中做错了吗? Or is it because there is no phone number for the other places? 或者是因为其他地方没有电话号码?

Here is the JSBin Link 这是JSBin链接

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Once you have the place id ( ChIJffZXHmsSdkgRBh8rxIhDbUw for the example in your question), you can do a place details request. 获得地点ID( ChIJffZXHmsSdkgRBh8rxIhDbUw中的示例为ChIJffZXHmsSdkgRBh8rxIhDbUw )后,您可以执行地点详细信息请求。

Make sure you include the Places library in your API call: https://maps.googleapis.com/maps/api/js?libraries=places 确保在API调用中包含Places库: https://maps.googleapis.com/maps/api/js?libraries=placeshttps://maps.googleapis.com/maps/api/js?libraries=places library https://maps.googleapis.com/maps/api/js?libraries=places places

function initialize() {

    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: new google.maps.LatLng(0, 0),
        zoom: 15
    });

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

    service.getDetails({
        placeId: 'ChIJffZXHmsSdkgRBh8rxIhDbUw'
    }, function(place, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {

            // Create marker
            var marker = new google.maps.Marker({
                map: map,
                position: place.geometry.location
            });

            // Center map on place location
            map.setCenter(place.geometry.location);

            // Get DIV element to display opening hours
            var phoneNumberDiv = document.getElementById("phone-number");

            // Create DIV element and append to opening_hours_div
            var content = document.createElement('div');
            content.innerHTML = 'Formatted phone number: ' + place.formatted_phone_number + '<br>'; 
            content.innerHTML += 'International phone number: ' + place.international_phone_number;

            phoneNumberDiv.appendChild(content);
        }
    });
}

initialize();

Have a look at the below working fiddle: 看看下面的工作小提琴:

JSFiddle demo JSFiddle演示

I was using the https://ubilabs.github.io/geocomplete/ to get the place_id but, this does not seem to get the correct place_id 我使用https://ubilabs.github.io/geocomplete/来获取place_id但是,这似乎没有得到正确的place_id

I tried https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-placeid-finder and this does work for me. 我尝试了https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-placeid-finder ,这对我有用。

So, when trying to get the place details its important to get the correct autocomplete and get the correct place_id 因此,在尝试获取位置详细信息时,重要的是获得正确的自动完成并获取正确的place_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谷歌放置 API getDetails() 服务不返回任何位置的评级 - Google places API getDetails() service not returning ratings for any location Google Places API-地址和电话号码? - Google Places API- Address and phone number? Google Maps API-格式化的电话号码在侧边栏的列表中显示为未定义 - Google Maps API- Formatted phone number shows up as undefined in List on side bar Google Maps:Google Places JS API:getDetails()不会返回整个地点的详细信息 - Google Maps: Google Places JS API: getDetails() not returning entire place details getDetails中的参考值错误-Google地方搜索 - error with reference value in getDetails - google places search Google Maps JavaScript API v3:多个地方的getDetails(请求,回调) - Google Maps JavaScript API v3: getDetails(request, callback) for multiple places Google Maps API place.url返回“未定义”吗? - Google Maps API places.url returns “undefined”? Google Places API - 地点详情请求未定义 - Google Places API - Places detail request undefined 为什么 Google Maps Places 只在 getDetails 方法中向我返回带有此类代码(例如:V83X+9G)的地点名称? - Why Google Maps Places only returns to me the name of the place with this type of code (e.g.: V83X+9G) in getDetails method? 我可以在 Google Places API 调用中使用格式化的地址吗 - Can I use a formatted address in Google Places API call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM