Skip to content

Geocode在开发环境获取位置成功,但是在release的时候报错 #244

@wqb2017

Description

@wqb2017

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}


)
}
}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions