我正在创建一个使用Google地理编码进行网站注册的网站。目前我使用的代码是服务器端,但我最好将其作为客户端脚本。我如何转换它:
$id= mysqli_real_escape_string($con, $_REQUEST["id"]);
$postcode= mysqli_real_escape_string($con, $_REQUEST["postcode"]);
$mapping = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=" . $postcode . "&sensor=false" );
$geocodes = json_decode($mapping);
if($geocodes->status == 'OK'){
$town = $geocodes->results[0]->address_components[1]->long_name;
$city = $geocodes->results[0]->address_components[4]->long_name;
$county = $geocodes->results[0]->address_components[2]->long_name;
$lat = $geocodes->results[0]->geometry->location->lat;
$lon = $geocodes->results[0]->geometry->location->lng;
}
else{...
此外,我希望能够过滤掉国家/地区,以便有人知道这个数字代表什么?
答案 0 :(得分:0)
以下是使用Google地理API获取邮政编码的坐标/位置信息的示例javascript(客户端)端代码。我在代码中硬编码了一个邮政编码用于演示。谷歌的结果显示在DisplayAddress()例程中。
<html>
<head>
</head>
<script>
// Show a locator on the map
//
function ShowLocation( postal )
{
// Get the geographic location
var url = "http://maps.google.com/maps/api/geocode/json?address=" + postal + "&sensor=false";
sendRequest( url, DisplayAddress );
}
function DisplayAddress( url, response )
{
alert( response );
}
// Make a REST based request
function sendRequest( url, callback )
{
var req = createXMLHTTPObject();
if (!req)
return;
req.open( "GET", url, true);
req.setRequestHeader('User-Agent','XMLHTTP/1.0');
req.onreadystatechange = function ()
{
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 206 && req.status != 304)
{
alert( req.status + " " + url );
return;
}
callback( url, req.responseText );
}
if (req.readyState == 4)
return;
req.send( null );
}
// Create object for making REST based request
function createXMLHTTPObject()
{
if (navigator.appName == "Microsoft Internet Explorer")
{
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
return http;
}
</script>
<body onload="ShowLocation( '98607' )">
</body>
</html>
Andrew,OpenGeoCode.Org的联合创始人