Php uses Baidu Map API for IP location and GPS location

Source: Internet
Author: User
Php uses the Baidu Map API for IP location and GPS location. Recently, a mobile-side webapp map application is developed, and the core content is Positioning. However, there are several methods for locating IP addresses, GPS positioning and base station positioning (this seems to be useless for webapp), then the core gps positioning and ip location are left. we know that html5 has a positioning API, however, the GPS data obtained by this API is hardware coordinates and cannot be directly displayed on the map. Later hundreds of LBS cloud See map IP location API and GPS coordinate conversion API, address: http://developer.baidu.com/map/ Baidu Map API call need to apply for KEY, here is not detailed introduction, I directly pasted two key methods. to facilitate frontend calls, the following format is used to return data:

{Address: "North West second flag Road, Haidian district, Beijing", province: "Beijing", city: "Beijing", street: "North West Second Flag", street_number: "", city_code: 131, lng: 116.3207676804, lat: 40.064084055578}
Core
 */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 ::$ _ instance = new self;} return self ::$ _ instance ;} /*** execute CURL request * @ author: xialei
 
  
* @ Param $ url * @ param array $ params * @ param bool $ encode * @ param int $ method * @ return mixed */private function 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, expires, '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 Mobil E/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 whether the IP address is valid if (! Filter_var ($ ip, FILTER_VALIDATE_IP) {throw new Exception ('IP Address illegal ');} $ 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 ); // if ($ data ['status']! = 0) {throw new Exception ($ data ['message']);} // return address information return array ('address' => $ data ['content'] ['address'], 'Vince '=> $ data ['content'] ['address _ detail'] ['Vince'], '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 positioning ** @ param $ lng * @ param $ lat * @ return array * @ throws Exception */public function locationByGPS ($ 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'] ['sssscomponent'] ['Province'], 'City' => $ data ['result'] ['address'] ['city'], 'Street '=> $ data ['result'] ['address'] ['street'], 'street _ number' => $ data ['result'] ['address'] ['street _ number'], 'City _ code' => $ data ['result'] ['citycode'], 'lng '=> $ data ['result'] ['location'] ['lng'], 'lat' => $ data ['result'] ['location'] ['lat']);}
 

The call method will not be mentioned. Just take a look at the source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.