【问题标题】:How Can You Reorder And Reorganize Coordinates From A KML File With PHP?如何使用 PHP 从 KML 文件中重新排序和重新组织坐标?
【发布时间】:2015-03-22 22:46:48
【问题描述】:

根据http://assemblysys.com/php-point-in-polygon-algorithm/,我可以取一组点组成一个多边形,并确定一个点是位于多边形内部还是外部。我还创建了一个 KML 文件,当 JavaScript 使用它来确定其中的点时,我的最终目标是让每个标记包含一个 MySQL 表内的额外数据集,该数据集将存储此数据以供以后使用,从而导致更快的地图加载时间。

查看 KML 文件,每个 Polygon 都有一组由“,0.0”分隔的坐标,这意义不大,因为根据上述文档,我只需要一组纬度和经度。此外,纬度和经度在不同的地方。以下是坐标的示例:

-86.1459875,39.8622513,0.0 -86.1459875,39.8555639,0.0 -86.1398077,39.8556628,0.0 -86.1398077,39.862218399999996,0.0 -86.1459870.>25.25,0.0

相反,我希望集合如下所示:

39.8622513 -86.1459875, 39.8555639 -86.1459875, 39.8556628 -86.1398077, 39.862218399999996 -86.1398077, 39.8622513 -86.1459875

下面是我的代码示例,但是我不确定是否有更简单的方法可以使用 PHP 来操作数组,而无需使用多个 for 循环或 foreach 循环。

$kml = 'document.xml'; 
//get the total number polygons 
$numPoly = count( $kml->Document[0]->Folder);
$newArray = array(); 
$a = 1; 
$explode;
$explode['coordinates']; 
for( $i=0; $i <= $numPoly; $i++)
{
    $numPlace = count( $kml->Document[0]->Folder[$i]->Placemark); //count   the number of placemarks there are 
for($z = 0; $z <= $numPlace; $z++) 
{
    $regionName = $kml->Document[0]->Folder[$i]->Placemark[$z]->name."</br>"; //grab each placemarks name 
    //get the cordinates inside of each placemark 
    $regionCords = $kml->Document[0]->Folder[$i]->Placemark[$z]->Polygon->outerBoundaryIs->LinearRing->coordinates;
    //print_r($regionCords); print "<br/><br/>";
    foreach($regionCords as $num2 => $region)
    {
        print $a;
        $explodeCords = array_splice( explode('|', str_replace(',0.0',' |', $regionCords) ), 0, -1 ) ;

        foreach ($explodeCords as $exploded)
        {
            $exploded = explode(',', $exploded);
            $exploded1 = $exploded[0];
            $exploded2 = $exploded[1];
            $explodedString = $exploded2.$exploded1.",";
            //echo $explodedString; echo "<br/><br/>";
        }
    }
    $numCordinates =  count($explodeCords['coordinates'] ); //returns the number of [lat,long] cords in each cordinate set
    if ( !empty( $regionCords ) )
    {
        $a++;

    }
}
}

请让我知道我是否走对了路,或者是否有其他方法可以使用 PHP 的主要数组函数轻松切换这两个数字。

【问题讨论】:

    标签: php arrays google-maps google-maps-api-3


    【解决方案1】:

    解决方案:在每个 foreach 循环之后,最好将新数组从循环中移除,因为如果要添加另一个 foreach 循环,可能会导致不需要的效果,例如将值附加到多个新数组.

    从 foreach 循环中删除值后,您始终可以使用 array_map 来切换坐标。有关这方面更完整的示例,请参阅以下 Github 存储库:https://github.com/jdmagic21/GMapParse

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多