添加電子圍欄窗看,首先需登錄高德地圖控制臺(tái),申請(qǐng)服務(wù)為web服務(wù)的key
添加圍欄點(diǎn)
public function tianjia()
{
$name = input('name');//傳入電子圍欄名稱
$points = input("points");//傳入坐標(biāo)點(diǎn),坐標(biāo)點(diǎn)用;號(hào)分隔
$url = "https://restapi.amap.com/v4/geofence/meta?key=你申請(qǐng)的key";
$data = array(
"name"=>$name,
"points"=>$points,
"enable"=>"true",
"valid_time"=>"2049-05-19",
"alert_condition"=>"enter;leave",
"repeat"=>"Mon,Tues,Wed,Thur,Fri,Sat,Sun"
);
$res = $this->posturl($url,$data);
$gid = $res['data']['gid'];//返回圍欄添加成功后gid
//對(duì)應(yīng)邏輯或存儲(chǔ)
return json(["code"=>'0',"msg"=>"添加成功","gid"=>$gid]);
}
更新圍欄點(diǎn)
public function gengxin()
{
$name = input('name');//傳入電子圍欄名稱
$gid = input("gid");//傳入圍欄gid參數(shù)
$points = input("points");//傳入坐標(biāo)點(diǎn)唆阿,坐標(biāo)點(diǎn)用;號(hào)分隔
$url = "https://restapi.amap.com/v4/geofence/meta?key=你申請(qǐng)的key&gid=".$gid."&method=patch";
$data = array(
"name"=>$name,
"points"=>$points,
"valid_time"=>"2049-05-19",
"alert_condition"=>"enter;leave",
"repeat"=>"Mon,Tues,Wed,Thur,Fri,Sat,Sun"
);
$res = $this->posturl($url,$data);
if($res['data']['status']==0)
{
return json(["code"=>'0',"msg"=>"更新成功"]);
}else{
return json(["code"=>'1',"msg"=>"更新失敗"]);
}
}
刪除圍欄點(diǎn)
public function shanchu()
{
$gid = input('gid');//傳入圍欄gid參數(shù)
$url = "https://restapi.amap.com/v4/geofence/meta?key=你申請(qǐng)的key&gid=".$gid."&method=delete";
$res = $this->posturl($url,$data);
return json(["code"=>'0',"msg"=>"刪除成功"]);
}
獲取指定圍欄的坐標(biāo),在地圖中渲染
public function chaxun()
{
$gid = input('gid');傳入圍欄gid參數(shù)
if($gid)
{
$url = "https://restapi.amap.com/v4/geofence/meta?key=你申請(qǐng)的key&gid=" . $gid;
$res = $this->geturl($url);
$point = $res['data']['rs_list'][0]['points'];
$points = explode(';',$point);//將坐標(biāo)格式轉(zhuǎn)化為數(shù)組
return json(["code"=>0,"msg"=>'成功',"points"=>$points]);
}else{
return json(["code"=>0,"msg"=>'暫無(wú)電子圍欄']);
}
}
獲取所有圍欄坐標(biāo)锈锤,在地圖中渲染
public function chaxuns()
{
$url = "https://restapi.amap.com/v4/geofence/meta?key=你申請(qǐng)的key";
$res = $this->geturl($url);
return json(["code"=>0,"msg"=>'成功',"data"=>$res ]);
}
檢查坐標(biāo)是否可匹配到圍欄點(diǎn)
public function findditu()
{
$lat = input("lat");//緯度
$lng = input('lng');//經(jīng)度
$url = "https://restapi.amap.com/v4/geofence/status?key=你申請(qǐng)的key&diu=你手機(jī)的IMEI號(hào)&locations=".$lng.",".$lat.",".time();
$res = $this->geturl($url);
if(count($res['data']['fencing_event_list'])>0)
{
$gid = $res['data']['fencing_event_list'][0]['fence_info']['fence_gid'];
//返回圍欄gid
return json(["code"=>0,"msg"=>'成功',"gid"=>$gid ]);
}else{
return json(["code"=>0,"msg"=>'未匹配到圍欄']);
}
}
POST請(qǐng)求方法
public function posturl($url,$data){
$data = json_encode($data);
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output,true);
}
GET請(qǐng)求方法
public function geturl($url){
$headerArray =array("Content-type:application/json;","Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
return $output;
}