Users concerned about the micro-credit after the public number, you can reply to the user's location (Tencent map) to the public number, extraction location information latitude and longitude, into the latitude and longitude of Baidu. Then according to latitude and longitude, call the Baidu Map API, back to the nearby radius of 2KM within the "hotel" "hotel" (can be customized) and other information. Call the Baidu API, need to obtain apikey, if not, please go to the Baidu Developer Center to register and apply.
First, test the data returned by the interface with a set of latitude and longitude:
<?php/** the keyword **/header (' Content-type:text/html;charset=utf-8 ') based on a set of latitude to find within 2 kilometres of the vicinity; --------First step: Convert latitude and longitude----//reference Links: Http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition $
location_x = 23.134521;
$Location _y = 113.358803; $url = "http://api.map.baidu.com/geoconv/v1/?coords= $Location _x, $Location _y&from=3&to=5&ak=
Here fill in your Apikey ";
$res = file_get_contents ($url);
$res = Json_decode ($res, true);
Users send Tencent Soso map location information after conversion, get the latitude and longitude of Baidu map $x = $res [' Result '][0][' x '];
$y = $res [' result '][0][' y ']; ---------The second step: according to the latitude and the key words to obtain nearby information----------//reference Links: http://developer.baidu.com/map/index.php?title=webapi/guide/ Webservice-placeapi $url = "http://api.map.baidu.com/place/v2/search?ak= here fill in your apikey&output=json&query=". UrlEncode ("Hotel").
"&page_size=10&page_num=0&scope=2&location= $x, $y &radius=2000";
$res = file_get_contents ($url);
$res = Json_decode ($res, true);
echo "<pre>";
Print_r ($res);
echo "</pre>"; Extract the required information foreAch ($res [' Results '] as $k => $v) {$arr [$k] [' name '] = $v [' name '];
$arr [$k] ["address"] = $v [' Address '];
$arr [$k] [' img_url '] = ' http://misakaqnxt-public.stor.sinaapp.com/click.png ';
$arr [$k] [' detail_url '] = isset ($v [' detail_info '] [' detail_url '])? $v [' Detail_info '] [' Detail_url ']: ';
echo "<pre>";
Print_r ($arr);
echo "</pre>";
The returned data
If you fill in the correct apikey, then you should return the above data. Next: In the micro-letter Interface Platform code (placed in your public domain namespace in the php script), judge the message type, and get latitude and longitude, call Baidu Map API, mosaic XML return can. Due to the Baidu API returned data, there is no "hotel" thumbnail, so I used a map of their own website.
In order to allow users to customize the search around the "hotel"
or "hotel" and other information, you can first let the user reply "Looking for xx", and then extracted xx, put into the session, and other users to return to the geographical location of the session. But after I set the session, I couldn't get the session out. So I use the kvdb of Sina Cloud service, of course you can also use Memcache or Redis cache way.
$which = mb_substr ($keyword, 0, 2, ' UTF-8 ');
ElseIf ($which = = "Find") {
$find = Str_replace ($which, "", $keyword);
Call Sina Cloud's Kvdb service
$kv = new saekv ();
$kv->init ();
$kv->set (' Find ', $find);
$CONTENTSTR = "Choose the expression next to the ' + ', send the location, you can find the place to find";
$RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
echo $resultStr;
Exit ();
}
ElseIf ($postObj->msgtype = = ' Location ') {/** * If the location is received, return to the nearby hotel *//
--------The first step: conversion of latitude and longitude-------//reference Links: http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition
$Location _x = $postObj->location_x;
$Location _y = $postObj->location_y; $url = "http://api.map.baidu.com/geoconv/v1/?coords= $Location _x, $Location _y&from=3&to=5&ak=
Here fill in your Apikey ";
$res = file_get_contents ($url);
$res = Json_decode ($res, true);
Users send Tencent Soso map location information after conversion, get the latitude and longitude of Baidu map $x = $res [' Result '][0][' x '];
$y = $res [' result '][0][' y '];
---------The second step: according to the latitude and longitude and keywords to obtain nearby information----------$kv = new SAEKV ();
Initialize the Kvclient object $kv->init (); $url = "http://api.map.baidu.com/place/v2/search?ak= here fill in your apikey&output=json&query=". UrlEncode ($kv->get (' Find ')). "&page_size=10&page_num=0&scope=2&location= $x, $y &radius=2000 ";
$res = file_get_contents ($url);
$res = Json_decode ($res, true);
Extract information foreach ($res [' Results '] as $k => $v) {$arr [$k] [' name '] = $v [' name '];
$arr [$k] ["address"] = $v [' Address '];
$arr [$k] [' img_url '] = ' http://misakaqnxt-public.stor.sinaapp.com/click.png ';
$arr [$k] [' detail_url '] = isset ($v [' detail_info '] [' detail_url '])? $v [' Detail_info '] [' Detail_url ']: '; }//--------step three: concatenation of XML strings--------$head = "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><!
[cdata[news]]></msgtype> <ArticleCount>10</ArticleCount> <Articles> ";
$items = "";
foreach ($arr as $v) { $items. = "<item> <title><! [cdata[]. $v [' name ']. :". $v [' Address ']. "]]></title> <description><! [cdata[]. $v [' Address ']. "]]></description> <picurl><! [cdata[]. $v [' Img_url ']. "]]></picurl> <url><! [cdata[]. $v [' Detail_url '].
"]]></url> </item>";
} $foot = "</Articles></xml>"; $res = $head. $items.
$foot;
$RESULTSTR = sprintf ($res, $fromUsername, $toUsername, $time);
Echo $resultStr;
Exit ();
}
If you do not understand how to use the code, you can refer to my previous article: simple text reply query micro-Letter Select article
After the user has paid attention to the public number, he replied to find a hotel and then returned to the location to get the hotel information nearby.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.