【发布时间】: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