微信公众号最佳实践 ( 9.6)地图导航

地图导航

随着交通经济的发展,人们的活动区域越来越大,不认识路,找不到目的地的情况也屡有发生,地图导航的功能可以帮用户导航到目的地。

微信公众号最佳实践 ( 9.6)地图导航_第1张图片
微信公众号最佳实践 ( 9.6)地图导航_第2张图片
微信公众号最佳实践 ( 9.6)地图导航_第3张图片微信公众号最佳实践 ( 9.6)地图导航_第4张图片

高德地图

  • 高德地图是中国领先的数字地图内容,导航和位置服务解决方案提供商。高德拥有中国领先的电子地图数据库,并拥有多个“甲级资质”,在汽车导航、政府和企业应用,互联网及移动互联网位置服务应用三大业务领域,均处于市场领先地位
  • 高德地图API是一组基于云的服务接口,包括互联网地图API和手机地图API。
    为了方便企业和个人开发者使用高德地图,高德建立了地图API频道https://lbs.amap.com/,该频道提供各种开发文档,参考手册,功能演示,和工具资源,供开发者下载使用。
  • 我们使用高德地图API完成导航功能,我们需要根据目的地起始地的坐标,然后再根据两地的坐标来进行导航。
  • 我们使用高德地图JavaScript API
    中的“鼠标拾取地图坐标”功能来获得目的地的坐标,该功能的地址为:https://lbs.amap.com/api/javascript-api/example/map/click-to-get-lnglat/
高德地图API提供了线路规划功能,给出起点和终点,就可以搜索公交或驾车路线,接口示例如下:

公交线路导航接口:
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")
驾车线路导航接口:
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"
微信公众号最佳实践 ( 9.6)地图导航_第5张图片

index.php完整代码如下:


/*
    CopyRight 2018-
    All Rights Reserved
*/

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
    $wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if($tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }

    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $this->logger("R ".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                case "location":
                    $result = $this->receiveLocation($postObj);
                    break;
                default:
                    $result = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            $this->logger("T ".$result);
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    private function receiveEvent($object)
    {
        $content = "";
        switch ($object->Event)
        {
            case "subscribe":
                $content = "欢迎关注 德强1012 ";
                $content .= isset($object->EventKey)?("\n来自二维码场景 ".$object->EventKey):"";
                break;
            case "unsubscribe":
                $content = "取消关注";
                break;
        }
        $result = $this->transmitText($object, $content);
        return $result;
    }

    private function receiveText($object)
    {
        $keyword = trim($object->Content);
        $content = date("Y-m-d H:i:s",time());
        $result = $this->transmitText($object, $content);
        return $result;
    }

    private function receiveLocation($object)
    {
        $Pondbay = array("name" => "方倍工作室", "latitude" => "22.539394", "longitude" => "113.956246");

        $content[] = array("Title" =>"高德地图为您导航", 
                            "Description" =>"", 
                            "PicUrl" =>"", 
                            "Url" =>"");
        $content[] = array("Title" =>"点击图片查看公交线路导航", 
                            "Description" =>"", 
                            "PicUrl" =>"http://h.hiphotos.bdimg.com/wisegame/pic/item/1fd98d1001e9390186c2c97479ec54e737d196bc.jpg", 
                            "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");
        $content[] = array("Title" =>"点击图片查看驾车线路导航", 
                            "Description" =>"", 
                            "PicUrl" =>"http://b.hiphotos.bdimg.com/wisegame/pic/item/eeb1cb1349540923f48a42079058d109b2de49e3.jpg", 
                            "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");
        $result = $this->transmitNews($object, $content);
        return $result;
    }

    private function transmitText($object, $content)
    {
        $textTpl = "
                        
                        
                        %s
                        
                        
                    ";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }

    private function transmitNews($object, $arr_item)
    {
        if(!is_array($arr_item))
            return;

        $itemTpl = "    
                            <![CDATA[%s]]>
                            
                            
                            
                        
                    ";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

        $newsTpl = "
                        
                        
                        %s
                        
                        
                        %s
                        
                        $item_str
                    ";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
        return $result;
    }

    private function logger($log_content)
    {
        if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
            sae_set_display_errors(false);
            sae_debug($log_content);
            sae_set_display_errors(true);
        }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
            $max_size = 10000;
            $log_filename = "log.xml";
            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
            file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
        }
    }
}


?>

你可能感兴趣的:(微信公众号开发最佳实践)