微信小程序开发之真机测试 地图定位 map API 无法获取当前位置的问题

来源:互联网 发布:金投顾软件 编辑:程序博客网 时间:2024/06/05 11:56

今天做到地图定位的模块.模拟器肯定是获取不到位置的.下面为真机测试结果.

上图:



经纬度不说了.定位用的,我这里直接输入的数字定位.但是有许多问题

下图中scale是缩放比例,这个属性目前无效.后期微信团队应该会修复.毕竟现在刚开始公测.这样就导致我不管怎么修改scale,我的地图都是在默认的缩放比例.如上图.


markers中的rotate是图标的旋转角度.如果需要平行于屏幕的图标,那就设置为0吧.

另外,覆盖物的图标是可以修改的.给iconPath设置路径即可.

上一段代码:

[html] view plain copy
  1. <!--index.wxml-->  
  2. <button class="button" bindtap="getlocation" style="margin-top:30px" markers="{{markers}}">定位</button>  
  3. <map longitude="{{longitude}}" latitude="{{latitude}}" markers="{{markers}}" covers="{{covers}}" style="width: 100%; height: 300px;margin-top:30px"></map>  


[javascript] view plain copy
  1. //index.js  
  2. //获取应用实例  
  3. var app = getApp()  
  4. Page({  
  5.   data: {  
  6.     latitude: 0,//纬度  
  7.     longitude: 0,//经度  
  8.     speed: 0,//速度  
  9.     accuracy: 16,//位置精准度  
  10.     markers: [],  
  11.     covers: [],  
  12.   },  
  13.   onLoad: function () {  
  14.   },  
  15.   getlocation: function () {  
  16.     var markers = [{  
  17.       latitude: 31.23,  
  18.       longitude: 121.47,  
  19.       name: '浦东新区',  
  20.       desc: '我的位置'  
  21.     }]  
  22.     var covers = [{  
  23.       latitude: 31.23,  
  24.       longitude: 121.47,  
  25.       iconPath: '../images/car.png',  
  26.       rotate: 0  
  27.     }]  
  28.     this.setData({  
  29.       longitude: 121.47,  
  30.       latitude: 31.23,  
  31.       markers: markers,  
  32.       covers: covers,  
  33.     })  
  34.   }  
  35. })  


2.wx.getLocation(OBJECT) 获取当前位置API



红色框标出的就是经纬度,速度,精确度
用gch02返回的坐标可以直接打开地图.
具体api见文档



3.wx.openLocation(OBJECT) 查看位置

最简单粗暴的就是直接给经纬度定位.

代码:

[javascript] view plain copy
  1. /index.js  
  2. //获取应用实例  
  3. var app = getApp()  
  4. Page({  
  5.   data: {  
  6.     latitude: 0,//纬度  
  7.     longitude: 0,//经度  
  8.     speed: 0,//速度  
  9.     accuracy: 16,//位置精准度  
  10.     markers: [],  
  11.     covers: [],  
  12.   },  
  13.   onLoad: function () {  
  14.   },  
  15.   getlocation: function () {  
  16.     wx.getLocation({  
  17.       type: 'gcj02',  
  18.       success: function (res) {  
  19.         var latitude = res.latitude  
  20.         var longitude = res.longitude  
  21.         var speed = res.speed  
  22.         var accuracy = res.accuracy  
  23.         console.log("latitude:" + latitude)  
  24.         console.log("longitude:" + longitude)  
  25.         console.log("speed:" + speed)  
  26.         console.log("accuracy:" + accuracy)  
  27.         wx.openLocation({  
  28.           latitude: latitude,  
  29.           longitude: longitude,  
  30.           scale: 28  
  31.         })  
  32.       }  
  33.     })  
  34.   }  
  35. })  


真机测试 效果图:


0 0