【问题标题】:Why Google map getPlace() always returning Undefined object?为什么谷歌地图 getPlace() 总是返回未定义的对象?
【发布时间】:2017-05-12 13:55:02
【问题描述】:

这是我用于getPlace() 的函数,它总是返回undefined 对象:

var element = document.getElementById('hdnOldAddress');
var AutoCompleteAddress2;
element.value = 'Marketfair Mall 1916 Skibo Road  Fayetteville  NC 28314';

autocomplete2 = new google.maps.places.Autocomplete(element);


autocomplete2.addListener('place_changed', function () {
    var place2 = autocomplete2.getPlace();
    if (place2.address_components) {
        AutoCompleteAddress2 = [
            (place2.address_components[0] && place2.address_components[0].short_name || ''),
            (place2.address_components[1] && place2.address_components[1].short_name || ''),
            (place2.address_components[2] && place2.address_components[2].short_name || ''),
            (place2.address_components[3] && place2.address_components[3].short_name || ''),
            (place2.address_components[4] && place2.address_components[4].short_name || ''),
            (place2.address_components[5] && place2.address_components[5].short_name || ''),
            (place2.address_components[6] && place2.address_components[6].short_name || ''),
            (place2.address_components[7] && place2.address_components[7].short_name || ''),
            (place2.address_components[8] && place2.address_components[8].short_name || ''),
            (place2.address_components[9] && place2.address_components[9].short_name || ''),
            (place2.address_components[10] && place2.address_components[10].short_name || ''),
            (place2.address_components[11] && place2.address_components[11].short_name || ''),
            (place2.address_components[12] && place2.address_components[12].short_name || ''),
            (place2.address_components[13] && place2.address_components[13].short_name || ''),
            (place2.address_components[14] && place2.address_components[14].short_name || ''),
        ].join(' ');
    }
});
google.maps.event.trigger(autocomplete2, 'place_changed');

编辑

this.getPlace()替换为autocomplete2.getPlace()

【问题讨论】:

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


    【解决方案1】:

    这是我在上面使用的替代方法。我没有使用Autocomplete(),而是使用了AutocompleteService(),它返回predicted位置,并使用该位置地址将其传递给geocodingmap按预期显示位置。

    var service = new google.maps.places.AutocompleteService();
    service.getPlacePredictions({ input: 'Marketfair Mall 1916 Skibo Road  Fayetteville  NC 28314' }, function (predictions, status) {                    
        if (status == google.maps.places.PlacesServiceStatus.OK) {
            matchAddress = predictions[0].structured_formatting.secondary_text; 
            //Add further processing for geocoding for found address.
        }
    });
    

    【讨论】:

    • 这并没有完全解决我的问题,但帮助了我!那谢谢啦!!基本上我用 google.maps.places 替换了自动完成功能。
    • ...更完整的答案在这里stackoverflow.com/questions/48817397/…
    【解决方案2】:

    编辑:没关系,我错了。在这段代码中,this = autocomplete2。

    问题是 place_changed 没有被触发 ...


    您遇到的一个问题是“这个”不是您想要的。您不能在处理事件(在 place_changed 上)的函数中使用“this”(表示自动完成的实例)。在这些事件函数中,this 变量表示受影响的元素(点击、悬停……)

    试试这个:

    替换 var place2 = this.getPlace(); by var place2 = autocomplete2.getPlace();

    让我知道这是否解决了它(否则我们需要进一步研究)

    【讨论】:

    • 我尝试用 var place2 = autocomplete2.getPlace(); 替换,但没有用。得到同样的错误。
    • 我们手动将值设置为element(textbox),而不是从谷歌地图提供的自动建议下拉列表中进行选择。这就是我们没有上场的原因吗?
    • 问题是place_changed事件的触发。
    • 啊,好吧。如果您手动设置该值,这意味着不会触发 place_changed。你能解释一下你的目标是什么以及你为什么要这么做吗?
    • @EmmanuelDelay,我在这里测试这个地址https://maps.googleapis.com/maps/api/geocode/json?libraries=places&address=Marketfair Mall, Skibo Road, Fayetteville, NC, United States 总是给我ZERO_RESULT。但相同的地址可用于Autocomplete 并将标记放置在适当的位置。
    猜你喜欢
    • 2021-04-01
    • 2020-03-12
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    相关资源
    最近更新 更多