


WeChat public platform message interface development example of geographical location query for nearby businesses
Mar 31, 2017 pm 02:50 PM1. Obtain user address location message
The message and format when the user sends the location is as follows
Backend format:
<xml> <ToUserName><![CDATA[gh_680bdefc8c5d]]></ToUserName> <FromUserName><![CDATA[oIDrpjqASyTPnxRmpS9O_ruZGsfk]]></FLACFromUserName> <CreateTime>1359036619</CreateTime> <MsgType><![CDATA[location]]></MsgType> <Location_X>22.539968</Location_X> <Location_Y>113.954980</Location_Y> <Scale>16</Scale> <Label><![CDATA[中國(guó)廣東省深圳市南山區(qū)華僑城深南大道9789號(hào) 郵政編碼: 518057]]></Label> <MsgId>5837017832671832047</MsgId> </xml>
XML format explanation
ToUserName 消息接收方微信號(hào),一般為公眾平臺(tái)賬號(hào)微信號(hào) FromUserName 消息發(fā)送方微信號(hào) CreateTime 消息創(chuàng)建時(shí)間 MsgType 消息類型,地理位置為location Location_X 地理位置緯度 Location_Y 地理位置經(jīng)度 Scale 地圖縮放大小 Label 地理位置信息 MsgId 消息ID號(hào)
2. Obtain surrounding area information
Baidu Map Place API is a simple HTTP interface, used In order to return and query certain types of POI data in a certain area, and provide detailed query services for a single POI, users can use C#, C++, Java and other development languages ??to send HTTP requests and receive json, xml data.
Place API provides regional search POI services, POI details services, group purchase information retrieval services, and merchant group purchase details inquiries. The regional search POI service provides three regional search methods: within-city search, rectangular search, and circular area search.
We use circular area retrieval to implement the nearby search function.
place area retrieval POI service interface is as follows:
http://api.map.baidu.com/place/v2/search
Parameters | Whether it is required | Default value | Format example | Meaning |
location | is | None | ##38.76623, 116.43213
lat | Peripheral search center point, multiple points are not supported |
radius(r) | No | ##None2000 |
| Peripheral search radius, unit is meter|
is | None | Zhongguancun, ATM, Baidu Building | Search keywords, surrounding search and rectangular area Internal search supports union search of multiple keywords. Different keywords are separated by $ symbol, and up to 10 keyword searches are supported. For example: "Bank$Hotel". | |
No | None | Japanese BBQ/Teppanyaki, Chaowai Street | Tag items, combined with q to search | |
No | xml | json or xml | The output format is json or xml | |
is | 1 | 1, 2 | Search result detail level. If the value is 1 or empty, basic information is returned; if the value is 2, detailed POI information is returned | |
No | None | ##filter=industry_type:cater | |sort_name:price
|sort_rule:0
|price_section:100,200 |
industry_type: Industry type
sort_name: Sorting field
sort_rule: Sorting rule, the values ??are as follows: 0: from high to low, 1: from low to high; |
page_size | 否 | 10 | 10 | 范圍記錄數(shù)量,默認(rèn)為10條記錄,最大返回20條。多關(guān)鍵字檢索時(shí),返回的記錄數(shù)為關(guān)鍵字個(gè)數(shù)*page_size。 |
page_num | 否 | 0 | 0、1、2 | 分頁(yè)頁(yè)碼,默認(rèn)為0,0代表第一頁(yè),1代表第二頁(yè),以此類推。 |
ak | 是 | 無(wú) | E4805d16520de693a3fe707cdc962045 | 用戶的訪問密鑰,必填項(xiàng)。v2之前該屬性為key。 |
sn | 否 | 無(wú) |
| 用戶的權(quán)限簽名。 |
timestamp | 否 | 無(wú) |
| 設(shè)置sn后該值必填。 |
調(diào)用舉例如下:
http://api.map.baidu.com/place/v2/search?ak=MgBALVVeCd8THVBi6gPdvsvG&output=json&query=%E9%93%B6%E8%A1%8C&page_size=5&page_num=0&scope=2&location=39.915,116.404&radius=2000&filter=sort_name:distance
三、程序?qū)崿F(xiàn)
百度地圖類定義如下
class baiduMapClient { private $api_server_url; private $auth_params; public function __construct() { $this->api_server_url = "http://api.map.baidu.com/"; $this->auth_params = array(); $this->auth_params['key'] = "401f9a693dd267dd9a4661ec0895fb20"; $this->auth_params['output'] = "json"; } public function Geocoding_coordinate_address($location) { return $this->call("geocoder", array("location" => $location)); } //http://api.map.baidu.com/place/search?&query=眼鏡&location=39.915,116.404&radius=3000&output=json&key=37492c0ee6f924cb5e934fa08c6b1676 public function Place_search($query, $location, $radius) { return $this->call("place/search", array("query" => $query, "location" => $location, "radius" => $radius)); } protected function call($method, $params = array()) { $headers = array( "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: en-us,en;q=0.5", //"Accept-Encoding: gzip, deflate", "Referer: http://developer.baidu.com/" ); $params = array_merge($this->auth_params, $params); $url = $this->api_server_url . "$method?".http_build_query($params); if (DEBUG_MODE){echo "REQUEST: $url" . "\n";} $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $data = curl_exec($ch); curl_close($ch); $result = null; if (!empty($data)){ if (DEBUG_MODE){ echo "RETURN: " . $data . "\n"; } $result = json_decode($data); } else{ echo "cURL Error:". curl_error($ch); } return $result; } }
獲取附近的調(diào)用代碼如下:
function catchEntitiesFromLocation($entity, $x, $y, $radius) { $mapObj = new baiduMapClient(); $search = $mapObj->Place_search($entity, $x.",".$y, $radius); $results = $search->results; for ($i = 0; $i < count($results); $i++) { $distance = getDistance($x, $y, $results[$i]->location->lat, $results[$i]->location->lng); $shopSortArrays[$distance] = array( "Title"=>"【".$results[$i]->name."】<".$distance."M>".$results[$i]->address.(isset($results[$i]->telephone)?" ".$results[$i]->telephone:""), "Description"=>"", "PicUrl"=>"", "Url"=>""); } ksort($shopSortArrays);//排序 $shopArray = array(); foreach ($shopSortArrays as $key => $value) { $shopArray[] = array( "title" => $value["Title"], "description" => $value["Description"], "pic" => $value["PicUrl"], "url" => $value["Url"], ); if (count($shopArray) > 6){break;} } return $shopArray; }
計(jì)算兩坐標(biāo)之間距離如下
function getDistance($lat_a, $lng_a, $lat_b, $lng_b) { //R是地球半徑(米) $R = 6366000; $pk = doubleval(180 / 3.14169); $a1 = doubleval($lat_a / $pk); $a2 = doubleval($lng_a / $pk); $b1 = doubleval($lat_b / $pk); $b2 = doubleval($lng_b / $pk); $t1 = doubleval(cos($a1) * cos($a2) * cos($b1) * cos($b2)); $t2 = doubleval(cos($a1) * sin($a2) * cos($b1) * sin($b2)); $t3 = doubleval(sin($a1) * sin($b1)); $tt = doubleval(acos($t1 + $t2 + $t3)); return round($R * $tt); }
對(duì)于用戶的坐標(biāo)記錄,我們使用數(shù)據(jù)庫(kù)的方式來存儲(chǔ),
如果用戶發(fā)送查詢命令,則直接查詢,
function searchUserLocation($userWxid) { Global $mysql_host; Global $mysql_host_s; Global $mysql_port; Global $mysql_user; Global $mysql_password; Global $mysql_database; //查詢使用從庫(kù),支持SAE $mysql_table = "location"; $mysql_state = "SELECT * FROM ".$mysql_table." WHERE userWxid = \"".$userWxid."\""; $con = mysql_connect($mysql_host.':'.$mysql_port, $mysql_user, $mysql_password); if (!$con){ die('Could not connect: ' . mysql_error()); } mysql_query("SET NAMES 'UTF8'"); mysql_select_db($mysql_database, $con); $result = mysql_query($mysql_state); $location = array(); while($row = mysql_fetch_array($result)) { $location["x"] = $row["locationX"]; $location["y"] = $row["locationY"]; } mysql_close($con); if (isset($location["x"]) && $location["x"] != 0.0){ return $location; }else{ return "系統(tǒng)中沒有你的地理位置信息,請(qǐng)先發(fā)送位置給我!您不用擔(dān)心你的行蹤被泄漏,因?yàn)槟憧梢曰瑒?dòng)地圖,把別處的地址發(fā)送過來。"; } }
如果用戶發(fā)了位置,則進(jìn)行更新
function updateOrInsert($weixinid, $locationX, $locationY) { if (isset($_SERVER['HTTP_APPNAME'])){ $mysql_host = SAE_MYSQL_HOST_M; $mysql_host_s = SAE_MYSQL_HOST_S; //sae的從庫(kù) $mysql_port = SAE_MYSQL_PORT; $mysql_user = SAE_MYSQL_USER; $mysql_password = SAE_MYSQL_PASS; $mysql_database = SAE_MYSQL_DB; }else{ $mysql_host = "127.0.0.1"; $mysql_host_s = "127.0.0.1"; $mysql_port = "3306"; $mysql_user = "root"; $mysql_password = "root"; $mysql_database = "sae"; } $mysql_table = "location"; //INSERT INTO location VALUES("23s2s", 1122.2, 366.2) ON DUPLICATE KEY UPDATE locationX = 1122.2, locationY = 366.2; $mysql_state = "INSERT INTO ".$mysql_table." VALUES(\"".$weixinid."\", ".$locationX.", ".$locationY.") ON DUPLICATE KEY UPDATE locationX = ".$locationX.", locationY = ".$locationY.";"; var_dump($mysql_state); // $con = mysql_connect($mysql_host.':'.$mysql_port, $mysql_user, $mysql_password); if (!$con){ die('Could not connect: ' . mysql_error()); } mysql_query("SET NAMES 'UTF8'"); mysql_select_db($mysql_database, $con); $result = mysql_query($mysql_state); if ($result == true){ //return "你提交的位置為緯度:".$locationX.",經(jīng)度:".$locationY."。\n現(xiàn)在可以發(fā)送“附近”加關(guān)鍵字的命令查詢附近的目標(biāo),如“附近酒店”,“附近醫(yī)院”。"; return "已經(jīng)成功獲取你的位置。您不用擔(dān)心你的行蹤被泄漏,因?yàn)槟憧梢园亚Ю镏獾牡刂诽峤贿^來。\n現(xiàn)在可以發(fā)送“附近”加關(guān)鍵字的命令查詢附近的目標(biāo),如“附近酒店”,“附近醫(yī)院”。"; }else{ return "提交失敗,請(qǐng)重試。如果一直出現(xiàn)這樣的錯(cuò)誤,請(qǐng)給我們留言。"; } }
對(duì)于用戶發(fā)送的內(nèi)容,先提取坐標(biāo),然后進(jìn)行組合查詢
實(shí)現(xiàn)效果如下:
?
?
The above is the detailed content of WeChat public platform message interface development example of geographical location query for nearby businesses. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
