I use a google maps autocomplete. As this does not always return a zip and it is not possible to restrict to mutliple countrys, I then call a reverse geocode function to check for zip and country and to check if the result is within the wanted coutrys only:
var geocoder;
function in_array(needle, haystack) {
for(var i in haystack) {
if(haystack[i] == needle) return true;
}
return false;
}
function getPostal(loc, callback) {
var countrys = new Array('DE', 'AT', 'CH');
if (!geocoder) {
geocoder = new google.maps.Geocoder();
}
var geocoderRequest = {
'latLng': loc
}
geocoder.geocode(geocoderRequest, function(results, status) {
var country = "";
var zip = "";
if (status == google.maps.GeocoderStatus.OK) {
for(var i=0; i < results[0].address_components.length; i++) {
var component = results[0].address_components[i];
if(component.types[0] == "country") {
country = component.short_name;
}
if(component.types[0] == "postal_code") {
zip = component.short_name;
}
}
callback(1);
}
if (zip == "") callback(-1);
if (country == "") callback(-2);
if (!in_array(country, countrys)) callback(-3);
});
}
$(function() {
var checkPostal;
var input = document.getElementById('search');
var options = {
types: ["geocode"]
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
latlng = place.geometry.location;
//reverse
getPostal(place.geometry.location, function(checkPostal) { ;
if (checkPostal == 1) {
alert("OK");
} else {
if (checkPostal == -1) {
alert('no ZIP');
} else if (checkPostal == -2) {
alert('no country');
} else if (checkPostal == -3) {
alert('wrong country');
}
}
});
});
});
In case of choosing fe Berlin I get an 'OK' only. But choosing fe New York I first get an 'OK' and then 'wrong country'. In case of choosing fe Westport, Ireland, I even get 'OK', 'no zip' and 'wrong country'.
As I want to have further action after getting a single 'OK' only, how can II do that? Or questioned in an other way: how to get one single return value?
Thanks Fabian
You must leave the geocode
-callback when any of the conditions matches and execute callback(1)
at the end...so that it will be executed only when no condition matches:
geocoder.geocode(geocoderRequest, function(results, status) {
var country = "";
var zip = "";
if (status == google.maps.GeocoderStatus.OK) {
for(var i=0; i < results[0].address_components.length; i++) {
var component = results[0].address_components[i];
if(component.types[0] == "country") {
country = component.short_name;
}
if(component.types[0] == "postal_code") {
zip = component.short_name;
}
}
if (zip == "") {callback(-1);return;}
if (country == "") {callback(-2);return;}
if (!in_array(country, countrys)) {callback(-3); return;}
callback(1);
}else{
//do something when status is not OK
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.