Recently in a mobile phone WebApp map application, and the core content of course is positioning, but the location of the words there are several ways, IP location, GPS positioning, base station positioning (this seemingly webapp use), then the core of the GPS positioning and IP positioning, we know that HTML5 has a location API, but the GPS data that the API gets is hardware coordinates that can't be displayed directly 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 calls need to apply key, here is not specifically introduced, directly affixed to I wrote two key methods, To facilitate the foreground call, the returned data is 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}
Core class
*/class map{private static $_instance; Const REQ_GET = 1; Const REQ_POST = 2; /** * Singleton mode * @return Map */public static function instance () {if (!self::$_instance instanceof self) {self::$_in stance = new Self; } return self::$_instance; }/** * Perform a curl request * @author: Xialei
* @param $url * @param array $params * @param bool $encode * @param int $method * @return Mixed */Private functio N Async ($url, $params = Array (), $encode = true, $method = self::req_get) {$ch = Curl_init (); if ($method = = self::req_get) {$url = $url. '?' . Http_build_query ($params); $url = $encode? $url: UrlDecode ($url); curl_setopt ($ch, Curlopt_url, $url); } else {curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_post, true); curl_setopt ($ch, Curlopt_postfields, $params); } curl_setopt ($ch, Curlopt_referer, ' Baidu map REFERER '); curl_setopt ($ch, Curlopt_useragent, ' mozilla/5.0 (iPhone; CPU iPhone os 7_0 like Mac os X; En-US) applewebkit/537.51.1 (khtml, like Gecko) version/7.0 mobile/11a465 safari/9537.53 '); 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 Function Locationbyip ($IP) {//Check for fit Method 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); There is an error if ($data [' status ']! = 0) {throw new Exception ($data [' message ']); }//Return address information return array (' address ' = = $data [' content '] [' address '], ' province ' + $data [' content '] [' address_d Etail ' [' Province '], ' city ' = $data [' content '] [' address_detail '] [' City '], ' district ' and ' = ' $data [' content '] [' Add Ress_detail ' [' District '], ' street ' = $data [' content '] [' address_detail '] [' Street '], ' street_number ' and ' = ' $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 Positioning * @param $LNG * @pAram $lat * @return Array * @throws Exception */Public Function Locationbygps ($LNG, $lat) {$params = array (' Coor Dtype ' = ' wgs84ll ', ' Location ' and ' $lat. ',' . $LNG, ' ak ' = ' Baidu Map API KEY ', ' output ' = ' json ', ' pois ' and ' 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 '] [' Addresscomp Onent ' [' Province '], ' city ' = $data [' result '] [' addresscomponent '] [' City '], ' street ' = $data [' result '] [' Addre Sscomponent ' [' Street '], ' street_number ' = $data [' result '] [' addresscomponent '] [' street_number '], ' City_code ' =& gt; $data [' Result '] [' citycode '], ' lng ' = $data [' result '] [' location '] [' LNG '], ' lat ' + $data [' result '] [' Location ' [' lat ']); }}
The calling method is not said. Just a little bit of the source code to know.