需要一些帮助。 所以我正在使用Google API Snap To Road。 这是我的代码:
<?php
set_time_limit(0);
$jsonarr = array();
$today=date("Y-m-d");
$ACCOUNT=$_GET['ACCOUNT'];
$ticket_no=$_GET['ticket_no'];
require_once("config/msdb_connect.php");
$query = mssql_query("SELECT * FROM TblGps WHERE DOOR = '".$ticket_no."' AND DATETIME BETWEEN '".$today." 00:00:00' AND '".$today." 23:59:59' ORDER BY DATETIME DESC");
while($row = mssql_fetch_array($query))
{
//parameters
$lng=$row['LONG'];
$lat=$row['LAT'];
//$data = array('lat'=>$lat,'lng'=>$lng);
//echo $latlng = GetArray($data);
$jsonarr[] = array(
'id' => $row['ID'],
'account' => $row['ACCOUNT'],
'datetime' =>date("m/d/Y H:i:s",strtotime($row['DATETIME'])),
'loc' => $row['LOCATION'],
// 'speed' => $row['SPEED'],
'longi' => $lng,
'lat' => $lat,
'lac' => $row['LAC'],
'cid' => $row['CID'],
// 'engine' => $row['ENGINE'],
'remarks' => $row['REMARKS']
);
}
$path = GetArray($jsonarr);
$snailtrail=GetSnailTrail($path);
function GetArray($array){
$stringdata = "";
for($i = 0 ; $i <= count($array) - 1 ; $i++){
$rows = (object)$array[$i];
if($i == count($array) - 1) $stringdata .= $rows->lat.','.$rows->longi;
else $stringdata .= $rows->lat.','.$rows->longi.'|';
}
return $stringdata;
}
function GetSnailTrail($path){
$key='AIzaSyDRlfacNyHn7ZOsC0FzufqZ_rtQYfZD6wA';
$url='https://roads.googleapis.com/v1/snapToRoads?path='.$path.'&interpolate=true&key='.$key.' ';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST,"POST");
// curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$api_response = curl_exec($curl);
curl_close($curl);
$result = json_decode($api_response);
$result = (object)$result;
$result = (object)$result->location;
return $result;
}
所以我从数据库中获取经度和纬度。 然后,我将使用我的函数GetArray将其组合为GetSnailTrail函数中paerameter路径的多个记录。
所以在发送API之前会是这样的:
https://roads.googleapis.com/v1/snapToRoads?path=14.567467,121.030726|14.566950,121.030497|14.567447,121.031204|14.566947,121.030520|14.566947,121.030520|14.567590,121.031181|14.567590,121.031181|14.567377,121.031236|14.566947,121.030520|14.567447,121.031204|14.566947,121.030520|14.566947,121.030520|14.566947,121.030520|14.566947,121.030520|14.567590,121.031181|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567590,121.031181|14.567377,121.031236|14.567590,121.031181|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567466,121.030726|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567056,121.030569|14.567012,121.030539|14.567049,121.030568|14.567009,121.030545|14.567468,121.030728|14.567031,121.030549|14.567007,121.030545|14.567098,121.030403|14.567037,121.030548|14.567178,121.030624|14.566899,121.030486|14.566946,121.030520|14.567056,121.030569|14.567056,121.030569|14.567056,121.030569|14.566946,121.030520|14.566947,121.030522|14.566947,121.030522|14.566947,121.030522|14.567103,121.030594|14.566946,121.030520|14.566947,121.030522|14.566947,121.030524|14.567028,121.030556|14.566780,121.030502|14.566780,121.030502|14.567103,121.030594|14.567103,121.030594|14.566947,121.030522|14.566946,121.030520|14.566946,121.030520|14.566946,121.030520|14.566947,121.030524|14.566947,121.030524&interpolate=true&key=AIzaSyDRlfacNyHn7ZOsC0FzufqZ_rtQYfZD6wA
之后,API将返回如下结果:
{
"snappedPoints": [
{
"location": {
"latitude": 14.567361300000002,
"longitude": 121.03068420000001
},
"originalIndex": 0,
"placeId": "ChIJF6oagKzJlzMRlFWOLr4F1SI"
}
出于某种原因,我无法回复或得到回复,请在$jsonarr
上输入。
得到答复后该怎么办?
谢谢
答案 0 :(得分:1)
如果在浏览器中返回的url及相关数据访问返回数据的每一部分,我发现这个工作正常,假设$data
被分配了curl请求的响应。
curl请求是使用GET而不是POST完成的,并且响应在函数外部解码。以前,您的代码尝试访问$result->location
〜,而应该是$result->snappedPoints
function GetSnailTrail( $path=false ){
if( !$path )return false;
$key='AIzaSyDRlfacNyHn7ZOsC0FzufqZ_rtQYfZD6wA';
$url='https://roads.googleapis.com/v1/snapToRoads?path='.$path.'&interpolate=true&key='.$key.' ';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$path='14.567467,121.030726|14.566950,121.030497|14.567447,121.031204|14.566947,121.030520|14.566947,121.030520|14.567590,121.031181|14.567590,121.031181|14.567377,121.031236|14.566947,121.030520|14.567447,121.031204|14.566947,121.030520|14.566947,121.030520|14.566947,121.030520|14.566947,121.030520|14.567590,121.031181|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567590,121.031181|14.567377,121.031236|14.567590,121.031181|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567466,121.030726|14.567377,121.031236|14.567377,121.031236|14.567377,121.031236|14.567447,121.031204|14.567377,121.031236|14.567056,121.030569|14.567012,121.030539|14.567049,121.030568|14.567009,121.030545|14.567468,121.030728|14.567031,121.030549|14.567007,121.030545|14.567098,121.030403|14.567037,121.030548|14.567178,121.030624|14.566899,121.030486|14.566946,121.030520|14.567056,121.030569|14.567056,121.030569|14.567056,121.030569|14.566946,121.030520|14.566947,121.030522|14.566947,121.030522|14.566947,121.030522|14.567103,121.030594|14.566946,121.030520|14.566947,121.030522|14.566947,121.030524|14.567028,121.030556|14.566780,121.030502|14.566780,121.030502|14.567103,121.030594|14.567103,121.030594|14.566947,121.030522|14.566946,121.030520|14.566946,121.030520|14.566946,121.030520|14.566947,121.030524|14.566947,121.030524';
$data=GetSnailTrail( $path );
if( $data ) {
$json=json_decode( $data,true );
$points=$json['snappedPoints'];
foreach( $points as $key => $value ){
$obj=(object)$value;
$lat=$obj->location['latitude'];
$lng=$obj->location['longitude'];
$index=property_exists($obj,'originalIndex') ? $obj->originalIndex : 'n/a';
$id=$obj->placeId;
echo 'id:'.$id.' index:'.$index.' lat:'.$lat.' lng:'.$lng.'<br />';
}
}
最初构建路径时,可以进一步简化代码。如果要构造一个数组并在$jsonarr
循环中使用lat / lng填充,则不是处理while
数据的复杂函数,而是:
$pathdata=array();
while( $row = mssql_fetch_array( $query ) ){
$jsonarr[] = array(
'id' => $row['ID'],
'account' => $row['ACCOUNT'],
'datetime' => date( "m/d/Y H:i:s", strtotime( $row['DATETIME'] ) ),
'loc' => $row['LOCATION'],
'longi' => $row['LONG'],
'lat' => $row['LAT'],
'lac' => $row['LAC'],
'cid' => $row['CID'],
'remarks' => $row['REMARKS']
);
$pathdata[]="{$row['LAT']},{$row['LONG']}";
}
$path=implode('|',$pathdata);
然后,您可以使用选定的分隔符(implode
)|
数据来获得点数。
要使用javascript处理此问题,您可以使用以下内容进行处理:
echo "
<script type='text/javascript'>
/* \$data is already a json object, echo it in javascript and process */
var path={$data};
function processpath( path ){
var json=path.snappedPoints;
for( var o in json ){
try{
var point=json[ o ];
if( typeof( point )!='undefined' ){
var lat=point.hasOwnProperty('location') && point.location.hasOwnProperty('latitude') ? point.location.latitude : false;
var lng=point.hasOwnProperty('location') && point.location.hasOwnProperty('longitude') ? point.location.longitude : false;
var index=point.hasOwnProperty('originalIndex') ? point.originalIndex : 'n/a';
var id=point.hasOwnProperty('placeId') ? point.placeId : 'n/a';
if( lat && lng ) console.log( 'id:%s, index:%s, lat:%s, lng:%s', id, index, lat, lng );
}
}catch( err ){
console.warn( err );
continue;
}
}
}
processpath( path );
</script>";