Automatically populate a country and city with a zip code as well as an oposite

I am trying to make a simple form that requires an address and a zip code, so I was wondering if there is a way to automatically fill in these fields based on what the user has already typed.

So, for example, if he decides to enter only a zip code, the fields of the city and country will be filled in automatically and vice versa.

After searching for a while, I found these databases that I could use

But other than that, I'm not sure how to do this using PHP and MySQL. Any help would be greatly appreciated.

+4
source share
2 answers

* , , , $country $city, google api. , , .

$postcode=$_POST('postcode');
                if($postcode)
                {

                    $address = urlencode($postcode);
                    $url='http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false';

                    $ch = curl_init(); 
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                    curl_setopt($ch, CURLOPT_URL, $url);
                    $data = curl_exec($ch);
                    curl_close($ch);
                    $source = $data;
                    $obj = json_decode($source);
                    $lat = $obj->results[0]->geometry->location->lat;
                    $long = $obj->results[0]->geometry->location->lng;


                }
$longitude=$long;
$latitude=$lat;

$longitude $latitude . :.go https://developers.google.com/maps/documentation/geocoding/?csw=1

. latitude/longitude latLng.

 var geocoder;
  var map;
  var infowindow = new google.maps.InfoWindow();
  var marker;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(40.730885,-73.997383);
    var mapOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
  }

  function codeLatLng() {
    var input = document.getElementById("latlng").value;
    var latlngStr = input.split(",",2);
    var lat = parseFloat(latlngStr[0]);
    var lng = parseFloat(latlngStr[1]);
    var latlng = new google.maps.LatLng(lat, lng);//here you will get your country and city name
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          map.setZoom(11);
          marker = new google.maps.Marker({
              position: latlng,
              map: map
          });
          infowindow.setContent(results[1].formatted_address);
          infowindow.open(map, marker);
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }

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

: getLocations (latlng: GLatLng, callback: function)

, / . getLocations() Google, latlng .

Google, . , Placemark.

, , ; . , , GLatLng, .

+1

, @RoyalBg sais, " , ", userid

SELECT zip_code FROM locations WHERE country like '%$country%' and userid = 254
-1

All Articles