dongliling6336 2011-02-10 03:40
浏览 137
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • douqian2524 2011-02-10 09:26
    关注

    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("
    " , $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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开