H5 唤醒腾讯地图百度地图并导航

说明文档:

html 使用时只需要调用 goAddress();即可

控制器

public function baoyang()
{
          
   
    $jssdk = new JssdkwxService();
    $wxjssdk = $jssdk->getJssdk();
    $this->assign(wxjssdk,$wxjssdk);
    $this->display();
}
<?php
namespace Extend;

class JSSDK {
           
    
  private $appId;
  private $appSecret;
  private $jsapi_ticket;
  private $tzurl;

  public function __construct($appId, $appSecret,$jsapi_ticket,$tzurl=null) {
           
    
    $this->appId = $appId;
    $this->appSecret = $appSecret;
    $this->jsapi_ticket = $jsapi_ticket;
    $this->tzurl = $tzurl;
  }

  public function getSignPackage() {
           
    
    $jsapiTicket = $this->jsapi_ticket;

    // 注意 URL 一定要动态获取,不能 hardcode.
      if($this->tzurl){
          $url = str_replace(amp;,,$this->tzurl);
      }else{
          $protocol = (!empty($_SERVER[HTTPS]) && $_SERVER[HTTPS] !== off || $_SERVER[SERVER_PORT] == 443) ? "https://" : "http://";
          $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
      }
    $timestamp = time();
    $nonceStr = $this->createNonceStr();

    // 这里参数的顺序要按照 key 值 ASCII 码升序排序
    $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

    $signature = sha1($string);

    $signPackage = array(
      "appId"     => $this->appId,
      "nonceStr"  => $nonceStr,
      "timestamp" => $timestamp,
      "url"       => $url,
      "signature" => $signature,
      "rawString" => $string
    );
    return $signPackage; 
  }

  private function createNonceStr($length = 16) {
           
    
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }
}
经验分享 程序员 微信小程序 职场和发展