小程序百度地图使用2

最近需要做一个类似滴滴打车的叫车小程序,需要用到百度地图的一些方法

1)获取用户当前位置,并在页面显示当前位置名称,先上代码:

var that=this;
var qqmapsdk = new QQmap({
key: config.Config.key // 必填
});
wx.getLocation({
type: 'gcj02',//默认为 wgs84 返回 gps 坐标,gcj02 返回可用于wx.openLocation的坐标
 
success: function(res) {
console.log(res);
var maker=[{
iconPath: "../../img/marker.png",
id: 0,
latitude: res.latitude,
longitude: res.longitude,
28,
height: 32
}]
that.setData({
 
centerLat:res.latitude,
centerLon:res.longitude,
markers:maker
})
qqmapsdk.reverseGeocoder({
location: {
latitude: res.latitude,
longitude: res.longitude
},
success: function (addressRes) {
var address = addressRes.result.formatted_addresses.recommend;
city = addressRes.result.address_component.city;
that.setData({
originAddress: address
})
}
})
},
})
wx.getLocation是小程序自带的得到位置的函数,可以得到当前位置的经纬度,但是并不能得到当前位置的名称。需要注意他的type参数决定了返回的经纬度是什么格式,当得到经纬度之后就需要使用百度地图的sdk了
qqmapsdk.reverseGeocoder这个函数将经纬度转成文字描述,输入坐标返回地理位置信息和附近poi列表。关于这个接口的详细说明请看http://lbs.qq.com/qqmap_wx_jssdk/method-reverseGeocoder.html
 
原文地址:https://www.cnblogs.com/wcxjy/p/8608335.html