使用者關注了微信公衆号之後,可以回複使用者的地理位置(騰訊地圖)給公衆号,提取位置資訊的緯度和經度,轉化為百度的緯度和經度。
然後根據緯度和經度,調用百度地圖的API,傳回附近半徑2KM以内的“飯店”“旅館”(可以自定義)等資訊。調用百度的API時,需要擷取apiKEY,如果沒有,請到百度開發者中心去注冊和申請。
首先,用一組緯度和經度來測試接口傳回的資料:
<?php
/**根據一組經緯度查找附近2公裡以内的關鍵字**/
header('Content-type:text/html;charset=utf-8');
//--------第一步:轉換經緯度----
//參考連結: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=這裡填寫你的apikey";
$res = file_get_contents($url);
$res = json_decode($res, true);
//使用者發送騰訊的soso地圖位置資訊經過轉換之後,得到百度地圖的經緯度
$x = $res['result'][0]['x'];
$y = $res['result'][0]['y'];
//---------第二步:根據經緯度和關鍵詞獲得附近的資訊----------
//參考連結:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-placeapi
$url = "http://api.map.baidu.com/place/v2/search?ak=這裡填寫你的apikey&output=json&query=" . urlencode("飯店") . "&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>";
//提取所需的資訊
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>";
傳回的資料

如果你填寫了正确的apikey,那麼應該傳回了上面的資料了。
接下來:在微信的接口平台代碼(放在你的公網域名空間裡的PHP腳本)裡,判斷消息類型,并擷取緯度和經度,調用百度地圖API,拼接XML傳回即可。由于百度API傳回的資料裡,沒有“飯店”的縮略圖,是以我就用了自己網站的一張圖。
為了能夠讓使用者自定義查找周圍的“飯店”或“旅館”等資訊,可以先讓使用者回複“尋找XX”,然後提取出XX,放到session裡,等使用者再回複地理位置時取出session。但我設定了session之後,沒能取出session。是以我這裡用新浪雲的KVDB服務,當然你也可以用memcache或者redis等緩存方式。
$which = mb_substr($keyword, 0, 2, 'UTF-8');
elseif($which == "尋找"){
$find = str_replace($which, "", $keyword);
//調用新浪雲的KVDB服務
$kv = new SaeKV();
$kv->init();
$kv->set('find', $find);
$contentStr = "選擇表情旁邊的'+',發送位置,即可查找你要找的地方";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);
echo $resultStr;
exit();
}
elseif($postObj->MsgType == 'location'){
/**
* 如果是收到了地理位置消息,則傳回附近的飯店
*/
//--------第一步:轉換經緯度-------
//參考連結: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=這裡填寫你的apikey";
$res = file_get_contents($url);
$res = json_decode($res, true);
//使用者發送騰訊的soso地圖位置資訊經過轉換之後,得到百度地圖的經緯度
$x = $res['result'][0]['x'];
$y = $res['result'][0]['y'];
//---------第二步:根據經緯度和關鍵詞獲得附近的資訊----------
$kv = new SaeKV();
// 初始化KVClient對象
$kv->init();
$url = "http://api.map.baidu.com/place/v2/search?ak=這裡填寫你的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);
//提取資訊
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']:'';
}
//--------第三步:拼接XML字元串--------
$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();
}
如果你看不懂代碼怎麼用,可以參考我之前的文章:簡單的文本回複 查詢微信精選文章
使用者關注了公衆号之後,回複尋找飯店,然後回複地理位置之後,就能得附近的飯店資訊了。