【问题标题】:Google Places API getDetails formatted_phone_number returns undefinedGoogle Places API getDetails formatted_phone_number 返回未定义
【发布时间】:2016-10-01 22:01:16
【问题描述】:

我创建了这个 bin 来获取地点的详细信息 - 格式化地址和格式化电话号码。

但是,格式化后的电话号码显示为未定义的地点。

但它确实显示了诸如“Franklin D. Roosevelt Four Freedoms Park, 1 FDR Four Freedoms Park, Roosevelt Island, NY 10044, USA”等地点的电话号码

但是,大多数其他地方都返回未定义。

例如这个地方,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 在地图上显示电话号码。

但是,在 jsbin 页面中搜索“iStorage Limited 13 Alperton Ln, Perivale, Greenford, Middlesex UB6 8DH, UK”确实显示电话号码未定义。

我在代码中做错了吗?还是因为没有其他地方的电话号码?

Here is the JSBin Link

非常感谢任何帮助。

【问题讨论】:

  • 我觉得有些地方没有电话号码,你可以尝试在Google map搜索这个地方,看看它是否显示电话号码。如果没有,我猜你的 API 也不会得到电话。
  • 在编辑中添加了一个示例。

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


【解决方案1】:

我使用https://ubilabs.github.io/geocomplete/ 来获取place_id,但这似乎没有得到正确的place_id

我试过https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-placeid-finder,这对我有用。

因此,在尝试获取地点详细信息时,重要的是要获得正确的自动完成功能并获得正确的 place_id

【讨论】:

    【解决方案2】:

    获得地点 ID 后(ChIJffZXHmsSdkgRBh8rxIhDbUw 是您问题中的示例),您可以提出地点详细信息请求。

    确保在 API 调用中包含 Places 库: https://maps.googleapis.com/maps/api/js?libraries=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();
    

    看看下面的工作小提琴:

    JSFiddle demo

    【讨论】:

    • 我想我弄错了 place_id。 @MrUpsidown 你能帮我找出在我的小提琴中找出 place_id 的逻辑是否错误吗?
    • 似乎 geocomplete 没有返回正确的地点 ID。
    猜你喜欢
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多