2011-02-10 89 views
1

我想知道這是否可能與谷歌地圖。我使用kml文件在Google地圖上創建了2個小網格。它可能與kml和谷歌地圖

如何查找如果我的地址在網格1或2中列出的使用PHP的。需要幫助。

回答

1

我爲此寫了代碼,而不是英國地區的網格。

我必須使用DOMDocument::load()來讀取像XML這樣的KML文件,這使您可以讀取KML文件並獲取其包含的經度和緯度點。請記住,雖然我不得不稍微更改KML以使其起作用。建立在谷歌地圖自定義地圖後,首先點擊右鍵,複製谷歌地球鏈接 - 這將給像這樣

http://maps.google.co.uk/maps/ms?ie=UTF8&hl=en&vps=1&jsv=314b&msa=0&output=nl

你應該改變輸出kml,請訪問然後保存輸出,我在這裏省略了部分URL,因爲不會放棄我的地圖!

http://maps.google.co.uk/maps/ms?ie=UTF8&hl=en&vps=1&jsv=314b&msa=0&output=kml

然後我不得不刪除<kml>元素被刪除以下行

<kml xmlns="http://earth.google.com/kml/2.2"> 

而且

</kml> 

這將讓你只用<Document>元素其中包含的一點。然後使用DOMDocument讀取它並遍歷它以獲取它包含的座標。例如,您可以遍歷地標和它們的座標,創建一個polygin,然後與long相交。我用這個網站爲多邊形代碼http://www.assemblysys.com/dataServices/php_pointinpolygon.php。正是在這個例子中一個實用程序類:

$dom = new DOMDocument(); 
$dom->load(APPLICATION_PATH . self::REGIONS_XML); 

$xpath = new DOMXpath($dom); 
$result = $xpath->query("/Document/Placemark"); 

foreach($result as $i => $node) 
{ 
    $name = $node->getElementsByTagName("name")->item(0)->nodeValue; 

    $polygon = array(); 

    // For each coordinate 
    foreach($node->getElementsByTagName("coordinates") as $j => $coord) 
    { 
     // Explode and parse coord to get meaningful data from it 

     $coords = explode("\n" , $coord->nodeValue); 

     foreach($coords as $k => $coordData) 
     { 
       if(strlen(trim($coordData)) < 1) 
        continue; 

       $explodedData = explode("," , trim($coordData)); 

       // Add the coordinates to the polygon array for use in the 
       // polygon Util class. Note that the long and lat are 
       // switched here because the polygon class expected them 
       // a specific way around 
       $polygon[] = $explodedData[1] . " " . $explodedData[0]; 
     } 
    } 

    // This is your address point   
    $point = $lat . " " . $lng; 

    // Determine the location of $point in relation to $polygon 
    $location = $pointLocation->pointInPolygon($point, $polygon); 

    // $location will be a string, this is documented in the polygon link 
    if($location == "inside" || $location == "boundary") 
    { 
      // If location is inside or on the boundary of this Placemark then break 
      // and $name will contain the name of the Placemark 
      break; 
    } 
} 
+0

我會嘗試這一點,但我是一個菜鳥,超過這個東西一半是出於理解... – nomie 2011-02-10 15:57:05