根据上述接口,可以实现当微信公众号接收到用户的地理位置消息时,返回公交及自驾线路给用户。其相关代码如下。
1 private function receiveLocation($object) 2 { 3 $Pondbay = array("name" => "方倍工作室", "latitude" => "22.539394", "longitude" => "113.956246"); 4 5 $content = array("Title" =>"高德地图为您导航", 6 "Description" =>"", 7 "PicUrl" =>"", 8 "Url" =>""); 9 $content = array("Title" =>"点击图片查看公交线路导航", 10 "Description" =>"", 11 "PicUrl" =>"http:// h.hiphotos.bdimg.com/wisegame/pic/item/ 1fd98d1001e9390186c2c97479ec54e737d196bc.jpg", 12 "Url" =>"http:// mo.amap.com/?from=".$object->Location_X.", ".$object->Location_Y."(".$object->Label.")&to=".$Pondbay ['latitude'].",".$Pondbay['longitude']."(".$Pondbay['name'].") &type=1&opt=0&dev=1");13 $content = array("Title" =>"点击图片查看驾车线路导航", 14 "Description" =>"", 15 "PicUrl" =>"http:// b.hiphotos.bdimg.com/wisegame/pic/ item/eeb1cb1349540923f48a42079058d109b2de49e3.jpg", 16 "Url" =>"http:// mo.amap.com/?from=".$object->Location_ X.",".$object->Location_Y."(".$object->Label.")&to=".$Pondbay ['latitude'].",".$Pondbay['longitude']."(".$Pondbay['name'].") &type=0&opt=1&dev=1");17 $result = $this->transmitNews($object, $content);18 return $result;19 }
上述代码运行时,将返回一个多图文消息,分别显示标题、公交导航、驾车导航的消息。公交和驾车的图文消息的链接为高德地图的接口URL,接口中起始地址为用户发送的地理位置,目的地址为已定义的坐标地址。该程序运行后,界面如图24-7所示。
点击公交线路和驾车线路导航后,高德地图生成的线路规划如图24-8所示。
图24-7 接收地理位置信息
图24-8 公交线路和驾车线路规划