Auto fill country and city from zip code, and the oposite

感情迁移 提交于 2019-11-30 21:59:01

*This is very easy what u need to do is just get the zip code from the user and with that you can get the $country and $city using google api.here i give you an example of how i am getting longitude and latitude from the postalcode i.e. zip code.

$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;

than you can insert the above two variables $longitude and $latitude in your database.simple as that :) .go to the following link https://developers.google.com/maps/documentation/geocoding/?csw=1

now for getting the country name from longitude and latitude use reverse geocoding. supply a comma-separated latitude/longitude pair in the latLng parameter.

 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);
      }
    });
  }

read about reverse geocodng here and modify the code according to your needs https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding

Use : getLocations(latlng:GLatLng, callback:function)

This method performs reverse-geocoding, the conversion of a latitude/longitude pair into human-readable addresses. getLocations() sends a request to the Google geocoding service, asking it to return the address for the given latlng and pass the response in the given callback.

As this method requires a call to a Google server, you must also pass a callback method to handle the response. This response will contain a Status code, and if successful, one or more Placemark objects.

Note that this method may instead pass an addressable String, as indicated above; in that case, the service will do a standard geocode. If however, the first argument contains a GLatLng, the service will do a reverse-geocode.

You can do what @RoyalBg sais but if "based on what the user has already typed" you should add a column userid

SELECT zip_code FROM locations WHERE country like '%$country%' and userid = 254
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!