在componentDidMount中,初始化mapCtx。
let _this = this
this.mapCtx = Taro.createMapContext('container') //container是地圖顯示模塊id
Taro.getLocation({
type: 'wgs84',
success(res) {
_this.setState({
latitude = res.latitude,
longitude = res.longitude
})
}
})
//移動到定位到的位置 this.mapCtx.moveToLocation()
注意:定位要配置app.js下的config,添加permission
1 "permission": { 2 "scope.userLocation": { 3 "desc": "你的位置信息將用於小程序位置接口的效果展示" 4 } 5 }