大家好!
我正在尝试将OSM(Overpass Turbo)中的JSON / XML解析为php并尝试获取speedlimit值。我能够获得该值,但在此之前存在一些错误
"未定义的偏移量:第44行" 的C:\ xampp \ htdocs \ android_connect \ test-osm.php中的1,这是代码if ($temp[1]=="maxspeed")
< / p>
我想要的是:
首先:从OSM获得结果后,我打破了&#34; &lt; &#34;标记为名为&#39; resultArr&#39;
的数组第二:然后,我打破了结果#rr&#39;用&#34; &#34; &#34;标签到一个名为&#34; temp&#34;。
的数组中以下是php文件:
<?php
//$lat = isset($_GET['lat']) ? floatval($_GET['lat']) : "";
//$lng = isset($_GET['lng']) ? floatval($_GET['lng']) : "";
$lat = 24.883968;
$lng = 55.544899;
//$latm = -0.00015 + $lat;
$latm = 54.580460;
//echo $latm. "\n";
//$latp = 0.00015 + $lat;
$latp = 54.580860;
//echo $latp. "\n";
//$lngm = -0.00015 + $lng;
$lngm = 24.326180;
//echo $lngm. "\n";
//$lngp = 0.00015 + $lng;
$lngp = 24.336580;
//echo $lngp;
$json_url = 'http://overpass.osm.rambler.ru/cgi/interpreter';
$data = '<query type="way"> <bbox-query s="' . $lngm . '" w="' . $latm . '" n="' . $lngp . '" e="' . $latp . '"/> <!--this is auto-completed with the current map view coordinates.--> </query> <print/>';
$ch = curl_init( $json_url );
$options = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$resultArr = explode("<",$result); //Array without "<" tags!
foreach ($resultArr as $val) {
$temp = explode('"', $val); //Array without """ tags!
//print_r ($temp);
//Trying to check if temp[1] is maxspeed, then get the value of temp[3]...
if ($temp[1]=="maxspeed")
$speedlimit=$temp[3];
}
echo $speedlimit;
?>
注意:此处的评论仅用于检查目的......
我知道我正在做一些小错误,我希望有人能告诉我我做错了什么或引导我走向正确的方向!!
非常感谢!
答案 0 :(得分:1)
问题在于并非所有方式都包含名为maxspeed的标记,因此您还需要检查是否
isset($temp[1]) && $temp[1] == "maxspeed"
您还可以使用这样的simplexml:
$xml = simplexml_load_string($result);
foreach ($xml->way as $i) {
foreach ($i->tag as $tag) {
if ($tag['k'] == "maxspeed") {
$maxspeed = $tag['v'];
break;
}
}
}
echo $maxspeed;
答案 1 :(得分:1)
语法错误,也许?缺少IF语句的大括号?
if ($temp[1]=="maxspeed")
$speedlimit=$temp[3];
应该是:
if ($temp[1]=="maxspeed") {
$speedlimit=$temp[3];
}