微信端H5使用百度地图定位获取当前位置安卓定位不准

微信端H5使用百度地图定位获取当前位置安卓定位不准的问题解决如下:

使用微信端定位,签名方法看微信接口文档:

<script type="text/javascript">
		$.ajax({
	        type : "post",
	        url : "../user/sign.vc",
	        data : {
	            "url" : location.href.split('#')[0]
	        },
	        dataType : "json",
	        success : function(data) {
	            wx.config({ 
	                debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。   
	                appId: data.appId, // 必填,公众号的唯一标识
	                timestamp: data.timestamp, // 必填,生成签名的时间戳
	                nonceStr: data.nonceStr, // 必填,生成签名的随机串
	                signature: data.signature,// 必填,签名,见附录1
	                jsApiList: [
								'checkJsApi',
								'openLocation',
								'getLocation'
	                           ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 
	            }); 
	            wx.ready(function () {
	        		wx.checkJsApi({
	        		    jsApiList: [
	        		        'getLocation'
	        		    ],
	        		    success: function (res) {
	        		        if (res.checkResult.getLocation == false) {
	        		            alert('你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!');
	        		            return;
	        		        }
	        		    }
	        		});
	        		wx.error(function (res) {
	        			alert(res.errMsg);
	        		});
	        	});
	        }
		 });
	</script>`

传入坐标实现公交导航

	function getMap(pointLng,pointLat,site) {
			wx.getLocation({
    			type: 'wgs84', 
    		    success: function (res) {
    		        var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
    		        var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
    		        
    		      	//坐标转换完之后的回调函数
    		        translateCallback = function (data){
    		          if(data.status === 0) {
    		            map.centerAndZoom(data.points[0], 13);
        				map.enableScrollWheelZoom(true); 
        				var p1 = data.points[0];
        				var p2 = new BMap.Point(pointLng,pointLat);
        				var transit = new BMap.TransitRoute(map, {
        					renderOptions: {map: map, panel: "r-result"},
        					onResultsHtmlSet : function(){$("#r-result").show()}
        				});
        				transit.search(p1, p2);
    		          }
    		        }
    		        
    		        var ggPoint = new BMap.Point(longitude,latitude);
    		        var convertor = new BMap.Convertor();
    		        var pointArr = [];
    		        pointArr.push(ggPoint);
    		        convertor.translate(pointArr, 1, 5, translateCallback)
    		    },
    		    cancel: function (res) {
    		        alert('用户拒绝授权获取地理位置');
    		    },
    	        fail:function(){
    	           alert("用户不允许获取地理位置");
    	        }
    		});
		}

版权声明:本文为zenghui2013原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。