微信小程序获得当前定位

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

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....话说这线路....画的好随意阿....

猜你喜欢

转载自blog.csdn.net/weixin_41583535/article/details/80332539
今日推荐