微信小程序获得当前定位

一、获得当前定位(无路线显示)

1、map.xml

< map 
longitude= '{{longitudes}}' 
latitude= '{{latitudes}}' 
scale= '14'  
show-location style= "width: 100%; height: 400px;"
>

注释:
longitude: 中心经度(number),因是实时定位,具体值在js文件中定义  
latitude : 中心纬度(number)

                  scale 缩放级别(number) 默认:16 取值范围:5-18

markers : 标记点用于在地图上显示标记的位置(数组)

                  show-location-style:显示带有方向的当前定位点

2、map.js

Page({
/**
* 页面的初始数据
*/
data: {

},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log( 'mapload...');
var that = this
wx.getLocation({
type: 'wgs84',
success: function (res) {
console.log( '纬度' + res.latitude);
console.log( '经度' + res.longitude);
that.setData({
latitudes: res.latitude,
longitudes: res.longitude,
})
},
})
},
})

3、效果



二、获得当前定位(有路线显示)

1、map.xml

< map 
longitude= '{{longitudes}}' 
latitude= '{{latitudes}}' 
scale= '14'  
show-location style= "width: 100%; height: 400px;"
polyline ='{{polyline}}'
>

注释:
longitude: 中心经度(number),因是实时定位,具体值在js文件中定义  
latitude : 中心纬度(number)

                  scale : 缩放级别(number) 默认:16 取值范围:5-18

markers : 标记点用于在地图上显示标记的位置(数组)

                  show-location-style:显示带有方向的当前定位点

polyline: 路线(数组)

2、map.js

Page({
/**
* 页面的初始数据
*/
data: {

},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log( 'mapload...');
var that = this
wx.getLocation({
type: 'wgs84',
success: function (res) {
console.log( '纬度' + res.latitude);
console.log( '经度' + res.longitude);
that.setData({
latitudes: res.latitude,
longitudes: res.longitude,
polyline: [{
points: [
{
longitude: res.longitude, // 起始点经度
latitude: res.latitude // 起始点纬度
}, {
longitude: 112.9388700000, //目标地点经度
latitude: 28.2269300000 //目标地点纬度
// 可搜索网站 在线经纬度查询:http://www.gpsspg.com/maps.htm
// 在此网站输入自己的目标地址即可得到此地址的经度和纬度
}
],
color: "#000000AA", //线的颜色
width: 2, //线的宽度
dottedLine: true //是否为虚线
}],
})
},
})
}
})

3、效果图



 emmmm....话说这线路....画的好随意阿....

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值