html5 获取和使用坐标获取没有地图的地址

html5 getting and using coordinates to get address without map

我想获得比 IP 显示的更精确的用户地址,所以我使用了 HTML 地理编码器来获取经纬度 - 到目前为止还不错。但是我想从中获取地址而不显示任何地图只是街道地址 - 城市 - 国家作为纯文本在后台做一些工作。据我所知 google 反向地理编码总是与显示地图有关。那么有办法解决这个问题吗?我可以使用下面的 php 函数,但我无法按原样将 html 5 var 传递给 php。

php

function GetAddress( $lat, $lng )
    {   
        // Construct the Google Geocode API call
        //
        $URL = "http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=false";

        // Extract the location lat and lng values
        //
        $data = file( $URL );
        foreach ($data as $line_num => $line) 
        {
            if ( false != strstr( $line, "\"formatted_address\"" ) )
            {
                $addr = substr( trim( $line ), 22, -2 );
                break;
            }
        }

        return $addr;
    }

    $address = GetAddress();
    print_r($address);
?>

我将采用的方法类似于此...(经过测试并有效)

    <div id='geo_results'></div>
    <script type='text/javascript' charset='utf-8'>
        window.onload=function(){

            navigator.geolocation.getCurrentPosition( geo_success, geo_failure );

            function geo_success(pos){
                var lat=pos.coords.latitude;
                var lng=pos.coords.longitude;

                var xhr=new XMLHttpRequest();
                xhr.onreadystatechange = function() {
                    if( xhr.readyState==4 && xhr.status==200 ){
                        geo_callback.call( this, xhr.responseText );
                    }
                }
                xhr.open( 'GET', '/fetch.php?lat='+lat+'&lng='+lng, true );
                xhr.send( null );
            }
            function geo_failure(){
                alert('failed');
            }

            function geo_callback(r){
                var json=JSON.parse(r);
                /* process json data according to needs */
                document.getElementById('geo_results').innerHTML=json.results[0].formatted_address;
                console.info('Address: %s',json.results[0].formatted_address);

            }
        }
    </script>

并且 php 页面将通过 curl

将请求发送到 google
/* fetch.php */
<?php
    /* 
        Modified only to filter supplied variables to help avoid nasty surprises 
    */
    $options=array( 'flags' => FILTER_FLAG_STRIP_HIGH | FILTER_FLAG_ENCODE_HIGH );
    $lat=filter_input( INPUT_GET, 'lat', FILTER_SANITIZE_ENCODED, $options );
    $lng=filter_input( INPUT_GET, 'lng', FILTER_SANITIZE_ENCODED, $options );

    $url="http://maps.googleapis.com/maps/api/geocode/json?latlng={$lat},{$lng}&sensor=false";

    $curl=curl_init( $url );
    /* 
        If CURLOPT_RETURNTRANSFER is set to FALSE 
        there is no need to echo the response 
        at the end
    */
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
    curl_setopt( $curl, CURLOPT_AUTOREFERER, TRUE );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE );
    curl_setopt( $curl, CURLOPT_FRESH_CONNECT, TRUE );
    curl_setopt( $curl, CURLOPT_FORBID_REUSE, TRUE );
    curl_setopt( $curl, CURLINFO_HEADER_OUT, FALSE );
    curl_setopt( $curl, CURLOPT_USERAGENT, 'curl-geolocation' );
    curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 15 );
    curl_setopt( $curl, CURLOPT_TIMEOUT, 90 );
    curl_setopt( $curl, CURLOPT_HEADER, FALSE );
    $response=curl_exec( $curl );

    echo $response;

?>