2017-03-25 3 views
0

Ich versuche, eine Postleitzahl zu setzen, um Lat und Long Koordinaten zu erhalten und eine Markierung darauf zu setzen. Bis jetzt ist alles in Ordnung.Google Maps API Suche Postleitzahlen in einem bestimmten Land

Das Problem kommt, wenn ich eine PLZ-Eingabe gebe und es irgendwo in einem anderen Teil der Welt einen Marker macht.

Ex: Ich tippe 2975-435 und ich bekomme: https://maps.googleapis.com/maps/api/geocode/json?address=2975-435&key=YOURKEY

"formatted_address" : "Balbey Mahallesi, 435. Sk., 07040 Muratpaşa/Antalya, Turquia", 

Und ich will diese Postleitzahl machen nur in Portugal gesucht werden.

https://maps.googleapis.com/maps/api/geocode/json?address=2975-435+PT Auf diese Weise erhalte ich:

"formatted_address" : "2975 Q.ta do Conde, Portugal", 

Genau das, was ich wollte.

Das Problem ist, wie mache ich das in JS-Code? Hier ist der Code, den ich bis jetzt

function codeAddress() { 
    var lat = ''; 
    var lng = ''; 
    var address = document.getElementById("cp").value; 
    geocoder.geocode({ 'address': address}, 

    function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      lat = results[0].geometry.location.lat(); 
      lng = results[0].geometry.location.lng(); 
      //Just to keep it stored 
      positionArray.push(new google.maps.LatLng(lat,lng)); 
      //Make the marker 
      new google.maps.Marker({ 
       position:new google.maps.LatLng(lat,lng), 
       map:map 
      }); 

     }else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} 

Danke

Antwort

1

Um ein Ergebnis zu bestimmten Land zu beschränken Sie eine Komponente Filterung anwenden können, haben:

https://developers.google.com/maps/documentation/javascript/geocoding#ComponentFiltering

Also, Ihren JavaScript-Code wird sein

function codeAddress() { 
    var lat = ''; 
    var lng = ''; 
    var address = document.getElementById("cp").value; 
    geocoder.geocode({ 
     'address': address, 
     componentRestrictions: { 
      country: 'PT' 
     } 
    }, 

    function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      lat = results[0].geometry.location.lat(); 
      lng = results[0].geometry.location.lng(); 
      //Just to keep it stored 
      positionArray.push(new google.maps.LatLng(lat,lng)); 
      //Make the marker 
      new google.maps.Marker({ 
       position:new google.maps.LatLng(lat,lng), 
       map:map 
      }); 

     }else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} 

Sie können eine Komponente Filterung in Aktion finden Sie in der Geocoder-Tool:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D2975-435%26options%3Dtrue%26in_country%3DPT%26nfw%3D1

Hoffe, es hilft!

Verwandte Themen