1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//通过传递"经度"、"纬度"获取所在地理位置 //$lat: 纬度 //$lng: 经度 //$BaiDu_AK: 百度AK密钥 function GetLocation($lat, $lng, $BaiDu_AK) { $strLocation = ""; $gps_data = file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=$lng&y=$lat"); $arr = (array)json_decode($gps_data); $lat_t = $lng_t = ""; if (!$arr['error']) { $lat_t = base64_decode($arr['y']); $lng_t = base64_decode($arr['x']); } if (strlen($lat_t) > 0 && strlen($lng_t) > 0) { $xml = new DOMDocument(); $xml->load("http://api.map.baidu.com/geocoder/v2/?ak=$BaiDu_AK&callback=renderReverse&location=$lat_t,$lng_t&output=xml&pois=0"); $result_s = $xml->getElementsByTagName("result"); foreach($result_s as $result) { $formatted_address_s = $result->getElementsByTagName("formatted_address"); $strLocation = $formatted_address_s->item(0)->nodeValue; break; } } if (strlen($strLocation) == 0) { $xml = new DOMDocument(); $xml->load("http://maps.233.wiki/maps/api/geocode/xml?latlng=$lat,$lng&sensor=false&language=zh-CN"); $result_s = $xml->getElementsByTagName("result"); foreach($result_s as $result) { $formatted_address_s = $result->getElementsByTagName("formatted_address"); $strLocation = $formatted_address_s->item(0)->nodeValue; break; } } return $strLocation; } echo GetLocation("39.935377", "119.600492", "自己的百度AK"); |