2011-02-10 143 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