2013-11-04 140 views
4

我想做一个简单的形式,需要地址和邮政编码,所以我想知道是否有一种方法来自动填充这些字段基于用户有什么已经键入。从邮编自动填充国家和城市,并相反

因此,例如,如果他决定只输入邮政编码,城市和国家/地区将自动填充,反之亦然。

搜索过了一会儿,我发现这些数据库,我可能可能使用

但除此之外,我不知道如何做到这一点使用PHP和MySQL 。任何帮助,将不胜感激。

+1

制作一个包含'country'和'zip_code'列的表格。一旦一个国家被输入,通过ajax发送输入的国家到一个页面,它会像查询数据库那样查询数据库:“SELECT zip_code FROM zipcode WHERE country ='$ country''并填充zip_code字段的响应 –

+0

或者它是一个外部数据库想查询? – putvande

+0

我只有你在我提供的两个链接中可以找到的东西,唯一的其他选择是使用地理位置api作为Rishabh建议的。我正在考虑使用mysql从zip_code_database.csv获取数据并从那里开始... – ealiaj

回答

1

*这是很容易的什么ü需要做的是刚刚拿到从用户的邮政编码,您可以使用谷歌API获得$ country和$ city。 这里我给你一个例子我如何从邮政编码即邮政编码获取经度和纬度。

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

比你可以插入上述两个变量在database.simple $经度和纬度$作为:)。去下面的链接https://developers.google.com/maps/documentation/geocoding/?csw=1

现在从经度得到国名纬度使用反向地理编码。 在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); 
     } 
    }); 
    } 

读一下反向geocodng这里并修改代码https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding

用途:的getLocations(经纬度:的GLatLng,回调:函数)

此方法执行反向地理编码,转换将纬度/经度对映射到人类可读的地址。 getLocations()向Google地理编码服务发送请求,要求它返回给定latlng的地址并在给定回调中传递响应。

由于此方法需要调用Google服务器,因此您还必须传递回调方法来处理响应。该响应将包含一个状态码,如果成功,则包含一个或多个地标对象。

请注意,此方法可能会传递一个可寻址的字符串,如上所述;在这种情况下,该服务将执行标准的地理编码。但是,如果第一个参数包含GLatLng,则该服务将执行反向地理编码。

+0

OP要求国家/邮编,而不是经度/纬度。 – putvande

+0

@putvande我举了一个例子,说明如何实现他想要的,我的方法比其他人建议的要好得多。 –

+0

你可以改变它,所以它在你的例子中返回国家? – putvande

-1

你可以做什么@RoyalBg赛斯但如果“基于什么用户已经输入了”你应该添加一列userid

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

'表'locations'中的'userid'? :))))我不认为他需要根据用户的建议插入coutries/zipcode,但基于他给出的链接。 –

相关问题