可以用kml和谷歌地图吗

发布于 2024-10-16 11:26:38 字数 98 浏览 1 评论 0原文

我想知道谷歌地图是否可以实现这一点。我使用 kml 文件在谷歌地图上创建了 2 个小网格。

我如何使用 php 找出我的地址是否列在网格 1 或 2 中。请需要帮助。

I want to know if this is possible with google maps. I create a 2 small grids on google maps with kml file.

How can I find out using php of if my address is listed in grid 1 or 2. Need help please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2024-10-23 11:26:38

我为英国的地区编写了专门执行此操作的代码,但不是网格。

我必须使用 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 xmlns="http://earth.google.com/kml/2.2">

</kml>

这将只留下包含该点的 元素。然后,您可以使用 DOMDocument 读取此内容并迭代它以获取它包含的坐标。例如,您可以迭代地标及其坐标,创建一个多边形,然后将其与长形相交。我使用这个网站的多边形代码 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;
    }
}

I wrote code for doing exactly this, but rather than grids, for areas of the UK.

I had to read the KML file like XML using DOMDocument::load(), this enables you to read the KML file and get the longitude and latitude points it contains. Bear in mind though that I had to change the KML slightly for this to work. Firstly after building your custom map in Google Maps right click and copy the Google Earth link - this will give something like this

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

You should change the output to kml, visit then save the output, I have ommitted part of this URL here as not to give away my map!

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

I then had to remove the <kml> element be removing the following lines

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

And

</kml>

This will leave you with just the <Document> element which contains the point. You then read this using DOMDocument and iterate over it to get the coordinates it contains. For example you can then iterate over the Placemarks and their coordinates, creating a polygin and then intersecting that with the long. I used this site for the polygon code http://www.assemblysys.com/dataServices/php_pointinpolygon.php . It is a Util class in this example:

$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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文