【发布时间】:2011-02-10 17:26:19
【问题描述】:
我想知道这是否可以通过谷歌地图实现。我用 kml 文件在谷歌地图上创建了 2 个小网格。
如何使用 php 找出我的地址是否列在网格 1 或 2 中。需要帮助。
【问题讨论】:
我想知道这是否可以通过谷歌地图实现。我用 kml 文件在谷歌地图上创建了 2 个小网格。
如何使用 php 找出我的地址是否列在网格 1 或 2 中。需要帮助。
【问题讨论】:
我为英国的某些地区编写了代码,而不是网格。
我必须使用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 阅读此内容并对其进行迭代以获取其中包含的坐标。例如,您可以遍历地标及其坐标,创建一个多边形,然后将其与 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;
}
}
【讨论】: