-
Notifications
You must be signed in to change notification settings - Fork 69
Description
import React from "react";
import { View, Text, PermissionsAndroid } from "react-native";
import styles from "./style";
import { MapView, Location, Geocode } from 'react-native-baidumap-sdk'
var idx = 1
export default class BaiduMapCom extends React.Component {
constructor(props) {
super(props)
this.state = {
curLocation: {
latitude: 39,
longitude: 113
},
markeTitle: '当前位置'
}
}
async componentDidMount (){
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
await Location.init()
Location.setOptions({ gps: true, distanceFilter: 3 })
// 用户允许获取当前地理位置
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
Location.addLocationListener(curLocation => {
this.setState({
curLocation: curLocation
}, () => {
Geocode.reverse({
latitude: curLocation.latitude,
longitude: curLocation.longitude
}).then(res => {
console.log('Geocode', ++idx, res.description);
this.setState({
markeTitle: res.description
})
}).catch(err => {
this.setState({
markeTitle: '失败了'
})
// 失败
})
})
})
Location.start()
} else {
// 失败了
}
}
render () {
const {
curLocation,
markeTitle
} = this.state
return(
<MapView
style={{ flex: 1 }}
center={curLocation}
zoomLevel={18}
>
<MapView.Marker
selected
ref="markerRef"
key={Math.random()}
title={markeTitle}
coordinate={curLocation}
>
</MapView.Marker>
{JSON.stringify(curLocation)}
{markeTitle}
)
}
}
`