1,根据高德地图获取位置
public function getLocation($params)
{
$params['location'] = $params['longitude'] . ',' . $params['latitude'];
$args = [
'output' => "json",
'location'=> $params['location'],
'key' => config::get('im.gaodekey'),
'radius' => 1000,
'extensions'=>'base'
];
$url = "https://restapi.amap.com/v3/geocode/regeo";
try
{
$msg = client::get($url, ['query' => $args]);
$body = $msg->getBody();
$remainingBytes = $body->getContents();
$msg = json_decode($remainingBytes,1);
}
catch (ClientException $e)
{
$msg = $e->getResponse()->json();
throw new \LogicException("获取位置失败");
}
return $msg;
}
其中extensions为all的时候获取所有,base为获取基本信息
传经纬度 120.21,30.29
测试如下
{
"errorcode": 0,
"msg": "",
"data": {
"status": "1",
"regeocode": {
"addressComponent": {
"city": "杭州市",
"province": "浙江省",
"adcode": "330104",
"district": "江干区",
"towncode": "330104011000",
"streetNumber": {
"number": "83号",
"location": "120.209171,30.2897347",
"direction": "西",
"distance": "84.8803",
"street": "新风路"
},
"country": "中国",
"township": "彭埠街道",
"businessAreas": [
{
"location": "120.200272,30.271987",
"name": "城东",
"id": "330104"
},
{
"location": "120.234238,30.285965",
"name": "彭埠",
"id": "330104"
}
],
"building": {
"name": [],
"type": []
},
"neighborhood": {
"name": [],
"type": []
},
"citycode": "0571"
},
"formatted_address": "浙江省杭州市江干区彭埠街道杭州东站西广场杭州东站"
},
"info": "OK",
"infocode": "10000"
}
}
其中需要注意的是,如果地区为北京上海等直辖市,city为空
2,根据地址获取经纬度
address 例如山东青岛市城阳区天安数码城
public function location($params)
{
$args = [
'output' => "json",
'address'=> $params['address'],
'key' => config::get('im.gaodekey'),
];
$url = "https://restapi.amap.com/v3/geocode/geo";
$msg = client::get($url, ['query' => $args]);
$body = $msg->getBody();
$remainingBytes = $body->getContents();
$msg = json_decode($remainingBytes,1);
if ($msg['status'] == 1)
{
$location = $msg['geocodes'][0]['location'];
$location = explode(",",$location);
return ['longitude'=>$location[0],'latitude'=>$location[1]];
}
return false;
}
返回值如下
{
"errorcode": 0,
"msg": "",
"data": {
"longitude": "120.436682",
"latitude": "36.314377"
}
}