首页 > 技术文章 > 微信jssdk获取当前位置,以及打开微信地图

dashuai01 2015-09-25 11:10 原文

$(function() {
    var url = window.location.href;
    var userId = $("#userId").val();
    // var openId = $("#sessionOpenId").val();
    // alert("openId = " + openId);
    if ($("#path").val() != null && $("#path").val() != ''
            && typeof ($("#path").val()) != 'undefined') {
        if ($('#path').val().indexOf('msbh') != -1) {
            userId = $('#userId_share').val();
        }
    }
    $.post("http://www.quanqiuyouhui.com/kabao-api/getShareData.do", {
        'url' : url,
        'userId' : userId
    }, function(data) {
        data = eval("(" + data + ")");
        wx.config({
            debug : false,
            appId : data.map.appId,
            timestamp : data.map.timestamp,
            nonceStr : data.map.nonceStr,
            signature : data.map.signature,
            jsApiList : [ 'checkJsApi', 'onMenuShareTimeline',
                    'onMenuShareAppMessage', 'getLocation', 'openLocation',
                    'hideOptionMenu' ]
        });
        wx.ready(function() {
            // 1 判断当前版本是否支持指定 JS 接口,支持批量判断
            wx.checkJsApi({
                jsApiList : [ 'getNetworkType', 'previewImage' ],
                success : function(res) {
                    // 以键值对的形式返回,可用的api值true,不可用为false
                    // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                }
            });
            wx.hideOptionMenu();
            // 2. 分享接口
            wx.getLocation({
                type : 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
                success : function(res) {
                    // alert(JSON.stringify(res));
                    var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
                    // $("#latitude").val(latitude);
                    var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
                    // $("#longitude").val(longitude);
                    var speed = res.speed; // 速度,以米/每秒计
                    // $("#speed").val(speed);
                    var accuracy = res.accuracy; // 位置精度
                    // $("#accuracy").val(accuracy);
                },
                cancel : function(res) {
                    alert('用户拒绝授权获取地理位置');
                }
            });
            // 2.1 监听“分享给朋友”,按钮点击、自定义分享内容及分享结果接口
            $(".icon").on('click', function() {
          //注意:因为苹果手机默认浏览器将使用 attr('')获得的信息默认为字符串格式所以不会打开地图,需要先用parseFloat()将获得的经纬度转换一下如下
                var a = $(this).attr("latitude");
a
= parseFloat(a); var b = $(this).attr("longitude"); b = parseFloat(b); // alert(b); var c = $(this).attr("businessName"); // alert(c); var d = $(this).attr("address"); // alert(d); wx.openLocation({ latitude : a, longitude : b, name : c, address : d, scale : 14, infoUrl : 'http://www.quanqiuyouhui.com/kabao-api/pgyqr.do' }); }) // ---------------------------- }); }); });

 

推荐阅读