是否可以使用kml和谷歌地图

时间:2011-02-10 03:40:18

标签: php maps kml

我想知道谷歌地图是否可行。我使用kml文件在谷歌地图上创建了2个小网格。

如果我的地址列在网格1或2中,我如何找到使用php。需要帮助。

1 个答案:

答案 0 :(得分:1)

我为英国的某些地区编写了代码,而不是网格。

我必须使用DOMDocument::load()读取像XML这样的KML文件,这使您可以读取KML文件并获取它包含的经度和纬度点。请记住,虽然我必须稍微更改KML才能使用。首先,在Google地图中构建自定义地图后,右键单击并复制Google地球链接 - 这将提供类似这样的内容

  

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读取它并迭代它以获取它包含的坐标。例如,您可以迭代Placemarks及其坐标,创建一个polygin,然后将其与long相交。我将此站点用于多边形代码http://www.assemblysys.com/dataServices/php_pointinpolygon.php。在这个例子中它是一个Util类:

$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;
    }
}