网页调用手机公众号腾讯地图
php 函数配置
<?php // 步骤1.设置appid和appsecret $appid = ''; //此处填写绑定的微信公众号的appid $appsecret = ''; //此处填写绑定的微信公众号的密钥id // 步骤2.生成签名的随机串 function nonceStr($length){ $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJK1NGJBQRSTUVWXYZ';//随即串,62个字符 $strlen = 62; while($length > $strlen){ $str .= $str; $strlen += 62; } $str = str_shuffle($str); return substr($str,0,$length); } // 步骤3.获取access_token $result = http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret); $json = json_decode($result,true); $access_token = $json['access_token']; function http_get($url){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; } } // 步骤4.获取ticket $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token"; $res = json_decode( http_get ( $url ) ); $ticket = $res->ticket; // 步骤5.生成wx.config需要的参数 $surl = $_GET['link']; $ws = getWxConfig( $ticket,$surl,time(),nonceStr(16) ); function getWxConfig($jsapiTicket,$myurl,$timestamp,$nonceStr) { global $appid; $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$myurl"; $signature = sha1 ( $string ); $WxConfig["appid"] = $appid; $WxConfig["noncestr"] = $nonceStr; $WxConfig["timestamp"] = $timestamp; $WxConfig["url"] = $myurl; $WxConfig["signature"] = $signature; $WxConfig["rawstring"] = $string; return $WxConfig; } echo json_encode($ws); ?>
js 调用
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script> <script type="text/javascript"> $('.page-merch-list').on('touchstart','.showMap',function(){ var lat = $(this).attr('data-lat'); var lng = $(this).attr('data-lng'); var merName = $(this).attr('data-merName'); var addr = $(this).attr('data-addr'); showMap(lat,lng,merName,addr); }); function showMap(lat,lng,merName,addr){ var url = encodeURIComponent(location.href.split("#")[0]); $.ajax({ type: "get", url: "https://me.syb800.com/wechat.php?link=" + url, dataType: "json", contentType: "application/json; charset=utf-8", success: function(e) { var d = e.appid, i = e.timestamp, t = e.noncestr, n = e.signature; wx.config({ debug: 0, appId: d, timestamp: i, nonceStr: t, signature: n, jsApiList: ["openLocation"] }), wx.ready(function() { wx.openLocation({ latitude: parseFloat(lat),//目的地latitude longitude: parseFloat(lng),//目的地longitude name: merName, //目的地名 address: addr,//目的地地址 scale: 15//地图缩放大小,可根据情况具体调整 }); }) } }); //window.history.back(); //自动返回上一页面,因为地图页面重新打开了一个窗口,如果地图返回是空白页面的话,需加上这代码 } </script>