2015-07-12 72 views
0

我想得到更精確的用戶abit比IP顯示它的地址,所以我使用了HTML地理編碼器讓我的經驗豐富 - 迄今爲止這麼好。不過,我想從這個地址得到地址,而不顯示任何地圖只是街道地址 - 城市 - 國家作爲純文本在後臺做一些工作。就我看到的谷歌反向地理編碼總是涉及顯示地圖。那麼有沒有辦法解決這個問題?我可以使用下面的PHP函數,但我只是不能通過PHP的PHP 5,因爲它是。html5獲取和使用座標獲得地址沒有地圖

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

回答

2

我想借此將是一個類似於此...(測試和工程)

<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頁面將請求發送到谷歌的做法通過捲曲

/* 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; 

?> 
+0

即時通訊沒有得到任何東西,嘗試document.write(json)裏面的geo_回調檢查整個結果,但仍然沒有什麼 – mrahmat

+0

iam不知道爲什麼,它仍然沒有顯示任何東西:( – mrahmat

+0

控制檯告訴你什麼?你有沒有正確的路徑「fetch.php」? – RamRaider