- /**
- * 获取地址对应的坐标
- * @param $address
- * @return array
- */
- function getAddressPoint($address){
- $lng = 0;
- $lat = 0;
- $url = 'http://api.map.baidu.com/geocoder?output=json&address=' . urlencode($address);
- if(function_exists('curl_init')) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- $data = curl_exec($ch);
- curl_close($ch);
- }else{
- $data = file_get_contents($url,false,stream_context_create(array(
- "http"=>array(
- "method"=>"GET",
- "timeout"=>1
- ),
- )));
- }
- $data = json_decode($data,true);
- if($data && $data['status'] == 'OK' && isset($data['result']) && isset($data['result']['location']))
- {
- $lng = $data['result']['location']['lng'];
- $lat = $data['result']['location']['lat'];
- }
- return array($lng,$lat);
- }
- /**
- * 逆地理编码专属请求
- * User: Lg
- * Date: 2016/4/11
- * @param $address
- * @return array
- */
- function getAddress($lat,$lng){
- $location = $lat.','.$lng;
- $url = 'http://api.map.baidu.com/geocoder?callback=renderReverse&location='.$location.'&output=json&pois=1';
- if(function_exists('curl_init')) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- $data = curl_exec($ch);
- curl_close($ch);
- }else{
- $data = file_get_contents($url,false,stream_context_create(array(
- "http"=>array(
- "method"=>"GET",
- "timeout"=>1
- ),
- )));
- }
- $data = json_decode($data,true);
- if($data && $data['status'] == 'OK' && isset($data['result']) && isset($data['result']['addressComponent']))
- {
- $province = $data['result']['addressComponent']['province'];
- $city = $data['result']['addressComponent']['city'];
- $district = $data['result']['addressComponent']['district'];
- }
- return array($province,$city,$district);
- }