I have a form that contains a text field that is connected to the Google Places API. What I want to happen is that, when a recognized address is entered, I also get the lat/long location of the entered place and bash the values into other form fields, so that they can be saved on the backend without another API call. The following initialization code, along with a few debugging logs, is working fine for this (apologies for the stupid field IDs; I blame Drupal...)
我有一个表单,其中包含一个连接到Google Places API的文本字段。我想要发生的是,当输入一个识别的地址时,我还获得输入位置的纬度/经度位置,并将值压缩到其他表单字段中,这样它们就可以保存在后端而无需另外的API调用。以下初始化代码以及一些调试日志正常工作(对于愚蠢的字段ID道歉;我责怪Drupal ......)
function placesInitialize() {
var input = document.getElementById('edit-field-google-location-und-0-value');
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById('edit-field-google-location-und-0-value').value;
geocoder.geocode({ 'address': address }, function (results, status) {
console.log('inside geocoder.geocode');
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
console.log('lat and long = ' + latitude + ' and ' + longitude);
$('#edit-field-location-latitude-und-0-value').val(latitude);
$('#edit-field-location-longitude-und-0-value').val(longitude);
}
else {
console.log('GeocoderStatus not ok:');
console.log(google.maps.GeocoderStatus);
}
});
});
}
google.maps.event.addDomListener(window, 'load', placesInitialize);
Now, if the user enters something that is not recognized by Places, I want to clear the lat and long fields. That's where my problem is: how do I detect that this has happened? I had thought that this would have been signaled by google.maps.GeocoderStatus
being not OK, but those logs never fire. In fact, the "inside geocoder.geocode" message doesn't show up in this situation, either.
现在,如果用户输入的地方无法识别,我想清除纬度和长字段。这就是我的问题所在:如何检测到这种情况发生了?我原以为google.maps.GeocoderStatus不会发出这样的信号,但这些日志永远不会触发。实际上,“inside geocoder.geocode”消息也不会出现在这种情况下。
I've looked, but haven't yet found an event that google.maps might throw in this situation. I could hang something off the input field's blur
event, but I'm not sure what it would do (there would be stuff in the input field, just not valid stuff). I thought about having a semaphor that tracks whether the geocoder has been invoked, but I'd still need to detect the "invalid entry" state to catch the case where the user first enters something valid and then replaces it with something invalid.
我看过了,但还没有发现google.maps可能会在这种情况下抛出的事件。我可以在输入字段的模糊事件中挂起一些东西,但我不确定它会做什么(输入字段中会有东西,只是无效的东西)。我想过有一个跟踪地理编码器是否被调用的信号量,但是我仍然需要检测“无效条目”状态以捕获用户首先输入有效内容然后用无效内容替换它的情况。
Anyway: Any thoughts out there? Thanks!
无论如何:有什么想法吗?谢谢!
1 个解决方案
#1
0
place_change
only fires when a user selects one of the suggestions in the dropdown. When the places-library doesn't return suggestions, nothing will happen.
place_change仅在用户选择下拉列表中的某个建议时触发。当places-library没有返回建议时,什么都不会发生。
As the Autocomplete doesn't provide access to the the suggestions at all(unless you select one), you may request the suggestions via the Autocomplete-Service(then you'll be able to check if there are any suggestions).
由于自动填充功能根本无法访问建议(除非您选择一个),您可以通过自动填充服务请求建议(然后您就可以检查是否有任何建议)。
The Autocomplete-functionality you may implement then on your own based on the returned suggestions(e.g. via jQueryUI)
您可以根据返回的建议(例如,通过jQueryUI)自行实现自动完成功能
#1
0
place_change
only fires when a user selects one of the suggestions in the dropdown. When the places-library doesn't return suggestions, nothing will happen.
place_change仅在用户选择下拉列表中的某个建议时触发。当places-library没有返回建议时,什么都不会发生。
As the Autocomplete doesn't provide access to the the suggestions at all(unless you select one), you may request the suggestions via the Autocomplete-Service(then you'll be able to check if there are any suggestions).
由于自动填充功能根本无法访问建议(除非您选择一个),您可以通过自动填充服务请求建议(然后您就可以检查是否有任何建议)。
The Autocomplete-functionality you may implement then on your own based on the returned suggestions(e.g. via jQueryUI)
您可以根据返回的建议(例如,通过jQueryUI)自行实现自动完成功能