-2

我正在创建一个使用谷歌地理编码进行网站注册的网站。目前我使用的代码是服务器端的,但理想情况下我希望将它作为客户端脚本。我如何转换这个:

$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{...

另外,我希望能够过滤掉国家,所以有人知道这个数字代表什么吗?

4

1 回答 1

0

下面是使用 google geo API 获取邮政编码坐标/位置信息的示例 javascript(客户端)端代码。我在代码中硬编码了一个邮政编码以进行演示。来自 google 的结果显示在 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 的联合创始人

于 2013-11-06T18:29:09.380 回答