Recently in a mobile phone WebApp map application, and the core content of course is positioning, but there are several ways to locate the word, IP location, GPS positioning, base station positioning (this seemingly webapp use),
Then the core of GPS positioning and IP location, we know that HTML5 has a positioning API, but the API to get GPS data is hardware coordinates, can not be directly displayed on the map.
Later on the Baidu lbs cloud to see the map IP location API and GPS coordinates conversion API, address: http://developer.baidu.com/map/
Baidu Map API call need to apply key, here is not specifically introduced, directly affixed to I wrote two key methods, in order to facilitate the foreground call, return data in the following format:
{Address:"Xi er qi bei Lu, Haidian District, Peking City",Province:Beijing, City: "Beijing", Street: "West two flag North Road", street_number: " , city_ Code: 131, LNG: 116.3207676804, lat: 40.064084055578}
PHP code
<?PHP/** * @author xialei <[email protected]>*/classmap{Private Static $_instance; ConstReq_get = 1; ConstReq_post = 2; /** * Singleton mode * @return Map*/ Public Static functioninstance () {if(!self::$_instanceinstanceof Self) { Self::$_instance=NewSelf ; } returnSelf::$_instance; } /** * Perform curl Requests*/ protected functionAsync$api,Array $params) { $ch= Curl_init ($api.‘?‘.Http_build_query($params)); curl_setopt ($ch, Curlopt_returntransfer, 1); $resp= Curl_exec ($ch); Curl_close ($ch); return $resp; } /** * IP location * @param string $IP * @return Array * @throws Exception*/ Public functionLocationbyip ($ip) { //check if legitimate IP if(!filter_var ($ip,filter_validate_ip)) { Throw New Exception(' IP Address not valid '); } $params=Array( ' AK ' + ' Baidu Map API KEY ', ' ip ' = =$ip, ' coor ' = ' bd09ll '//Baidu Map GPS coordinates ); $api= ' HTTP://API.MAP.BAIDU.COM/LOCATION/IP '; $resp=$this->async ($api,$params); $data= Json_decode ($resp,true); //have errors if($data[' status ']! = 0) { Throw New Exception($data[' Message ']); } //Return address information return Array( ' Address ' =$data[' content '] [' Address '], ' province ' =$data[' content '] [' Address_detail '] [' Province '], ' city ' =$data[' content '] [' Address_detail '] [' City '], ' district ' =$data[' content '] [' Address_detail '] [' District '], ' street ' =$data[' content '] [' Address_detail '] [' Street '], ' street_number ' =$data[' content '] [' Address_detail '] [' Street_number '], ' city_code ' =$data[' content '] [' Address_detail '] [' City_code '], ' lng ' =$data[' content '] [' Point '] [' X '], ' lat ' =$data[' content '] [' Point '] [' Y '] ); } /** * GPS location * @param $lng * @param $lat * @return Array * @throws Exception*/ Public functionLocationbygps ($LNG,$lat) { $params=Array( ' Coordtype ' = ' wgs84ll ', ' location ' =$lat. ‘,‘ .$LNG, ' ak ' = ' Baidu Map API KEY ', ' output ' = ' json ', ' POIs ' + 0 ); $resp=$this->async (' http://api.map.baidu.com/geocoder/v2/',$params,false); $data= Json_decode ($resp,true); if($data[' status ']! = 0) { Throw New Exception($data[' Message ']); } return Array( ' Address ' =$data[' Result '] [' formatted_address '], ' province ' =$data[' Result '] [' Addresscomponent '] [' Province '], ' city ' =$data[' Result '] [' Addresscomponent '] [' City '], ' street ' =$data[' Result '] [' Addresscomponent '] [' Street '], ' street_number ' =$data[' Result '] [' Addresscomponent '] [' Street_number '], ' city_code ' =$data[' Result '] [' Citycode '], ' lng ' =$data[' Result '] [' Location '] [' LNG '], ' lat ' =$data[' Result '] [' Location '] [' Lat '] ); }}
PHP uses Baidu map API for IP location and GPS positioning