2016-03-24 85 views
-1

我已經查看了其他已回答的問題,但我仍不確定如何處理;計算兩個郵政編碼之間的距離

  1. 獲得英國郵政編碼數據,包括經度,緯度,電網-N和網格-E到我的數據庫

  2. 如果我使用一個API我怎麼做呢?我從哪裏開始?

  3. 我需要使用Pythagorus定理來計算兩個郵政編碼之間的距離嗎?
  4. 我的數據庫中有一張表,用於添加屬性。也許,有人添加一個屬性時,它可以將郵政編碼和其他信息(long,lat,grid-ref)一起添加到Postcodes表中,以便我可以計算出兩個郵政編碼之間的距離。

感謝

+0

無論是將太多可能的答案,還是很好的答案就太長了這種格式。請添加詳細信息以縮小答案集或隔離可以在幾個段落中回答的問題。我建議您找到一個開發論壇(可能是[Quora](http://www.quora.com/Computer-Programming) ?)來計算一般性。然後,當/如果您有特定的編碼問題,請回到StackOverflow,我們很樂意提供幫助。 –

+0

作爲一個提示,不要使用Pythagorus定理,除非距離非常近 - 你更可能需要大圓距離 –

+0

另外,值得一看[OS的OpenData](https://www.ordnancesurvey.co。 uk/opendatadownload/products.html)([My Society mirror](http://parlvid.mysociety.org/os/)) –

回答

0

我有一個類我專門用於此:

class Geocode 
{ 
    /** 
    * Work out the distance between two sets of lat/lng coordinates as the crow flies. 
    * 
    * @param float $lat1 
    * @param float $lng1 
    * @param float $lat2 
    * @param float $lng2 
    * 
    * @return float 
    */ 
    public static function distance($lat1 = 0.0, $lng1 = 0.0, $lat2 = 0.0, $lng2 = 0.0) { 
     $theta = $lng1 - $lng2; 
     $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
     $dist = acos($dist); 
     $dist = rad2deg($dist); 
     return $dist * 60 * 1.1515; 
    } 

    /** 
    * Get the lat/lng coordinates for an address. 
    * 
    * @param string $address 
    * 
    * @return stdClass 
    */ 
    public static function convert($address = '') 
    { 
     $address = str_replace(" ", "+", urlencode(str_replace(PHP_EOL, ', ', $address))); 
     $url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&region=uk&sensor=false"; 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $response = json_decode(curl_exec($ch), TRUE); 

     if($response['status'] != 'OK') { 
      return (object) ['status' => $response['status']]; 
     } 
     $geo = $response['results'][0]['geometry']; 

     return (object) [ 
      'lat'  => $geo['location']['lat'], 
      'lng'  => $geo['location']['lng'], 
      'status' => $response['status'] 
     ]; 
    } 
} 
相關問題