Google API getPlace from autocomplete return 手动触发时未定义
Google API getPlace from autocomplete return undefined when firing manually
我希望在手动触发此事件时能够 return 来自 autocomplete.getPlace()
的数据。
基本上,我有一个函数可以获取 Lat/Lng
中的用户位置,之后我想获取用户的 getPlace(),因为有更多有用的数据,例如 place_id、照片 (最想要的)、参考资料等
到目前为止,我几乎可以做任何事情,我什至可以手动触发事件,但它 return undefined
。这是我目前的代码:
var latLng = response; //Comes from another function
geocoder.geocode({'location': latLng}, function(results, status) {
if (status === 'OK') {
if (results[1]) {
var fullAddress = results[0].formatted_address;
var input = document.createElement("input");
input.setAttribute("value", fullAddress);
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var thisPlace = autocomplete.getPlace();
console.log(thisPlace);
});
google.maps.event.trigger(autocomplete, 'place_changed');
}
}
});
此处一切正常,但 autocomplete.getPlace()
中的 return 是 undefined
。
如果运行功能相同,但来自本机自动完成元素,则return是正确的。
我做错了什么?
您似乎想要 JavaScript 等同于 Android Place Picker. There is no such widget in the Places library. Consider filing a feature request。
您当前的方法似乎遵循以下思路:
- 将用户 latlng 反向地理编码到最近的地址。
- 获取该地址的地点。
在第 1 步中使用 geocoder.geocode()
是可以的,但是您需要注意要输入的结果:如果第一个结果是公共汽车站,它的地址将没有用。
你最好 Radar Search, to search for the nearest places to the original Lat/Lng input. This also avoids issues that may come from reverse geocoding snapping the user location to a not-so-near address. Then you'll need to issue a number of Details 请求获取最近的 N 个地点的名称(和其他详细信息)。
在第 2 步中使用自动完成功能效果不佳。自动完成具有多个地点的地址很少会列出所有地点。
此外:不要手动触发 place_changed
,这不是它的工作原理。
place_changed
事件由可用的 Autocomplete class only after making a PlaceResult 触发。如果用户从自动完成预测中选择了一个地点,这需要调用地点详细信息服务,以检索您实际想要获取的详细信息。
This event is fired when a PlaceResult is made available for a Place the user has selected.
If the user enters the name of a Place that was not suggested by the control and presses the Enter key, or if a Place detail request fails, a place_changed event will be fired that contains the user input in the name property, with no other properties defined.
如果您手动触发 place_changed
,则不会为您准备好 PlaceResult
,这就是为什么 autocomplete.getPlace()
只会 return undefined
。
我希望在手动触发此事件时能够 return 来自 autocomplete.getPlace()
的数据。
基本上,我有一个函数可以获取 Lat/Lng
中的用户位置,之后我想获取用户的 getPlace(),因为有更多有用的数据,例如 place_id、照片 (最想要的)、参考资料等
到目前为止,我几乎可以做任何事情,我什至可以手动触发事件,但它 return undefined
。这是我目前的代码:
var latLng = response; //Comes from another function
geocoder.geocode({'location': latLng}, function(results, status) {
if (status === 'OK') {
if (results[1]) {
var fullAddress = results[0].formatted_address;
var input = document.createElement("input");
input.setAttribute("value", fullAddress);
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var thisPlace = autocomplete.getPlace();
console.log(thisPlace);
});
google.maps.event.trigger(autocomplete, 'place_changed');
}
}
});
此处一切正常,但 autocomplete.getPlace()
中的 return 是 undefined
。
如果运行功能相同,但来自本机自动完成元素,则return是正确的。
我做错了什么?
您似乎想要 JavaScript 等同于 Android Place Picker. There is no such widget in the Places library. Consider filing a feature request。
您当前的方法似乎遵循以下思路:
- 将用户 latlng 反向地理编码到最近的地址。
- 获取该地址的地点。
在第 1 步中使用 geocoder.geocode()
是可以的,但是您需要注意要输入的结果:如果第一个结果是公共汽车站,它的地址将没有用。
你最好 Radar Search, to search for the nearest places to the original Lat/Lng input. This also avoids issues that may come from reverse geocoding snapping the user location to a not-so-near address. Then you'll need to issue a number of Details 请求获取最近的 N 个地点的名称(和其他详细信息)。
在第 2 步中使用自动完成功能效果不佳。自动完成具有多个地点的地址很少会列出所有地点。
此外:不要手动触发 place_changed
,这不是它的工作原理。
place_changed
事件由可用的 Autocomplete class only after making a PlaceResult 触发。如果用户从自动完成预测中选择了一个地点,这需要调用地点详细信息服务,以检索您实际想要获取的详细信息。
This event is fired when a PlaceResult is made available for a Place the user has selected. If the user enters the name of a Place that was not suggested by the control and presses the Enter key, or if a Place detail request fails, a place_changed event will be fired that contains the user input in the name property, with no other properties defined.
如果您手动触发 place_changed
,则不会为您准备好 PlaceResult
,这就是为什么 autocomplete.getPlace()
只会 return undefined
。