当前位置适用于long和lat null值,但不适用于IOS吗?

问题描述 投票:0回答:1

我想在IOS上获取我在地图上的当前位置,但是很长:null和lat:null,两者似乎都在android上获取了地图上的当前位置,但不适用于IOS,我该怎么做才能获得IOS上的当前位置?下面是代码,例如我的初始区域是lat:null和long:null,但是在android上我可以获取当前位置,但是在ios上我不可以

  class MapScreen extends Component {
  //Set the HeaderTitle screen
  static navigationOptions = props => {
    const placeName = props.navigation.getParam("placeName");
    return { headerTitle: placeName.toUpperCase() };
  };
  constructor(props) {
    super(props);
    //Initial State
    this.state = {
      lat: null,
      long: null,
      places: [],
      isLoading: false,
      placeType: "restaurant"
    };
  }
  componentDidMount() {
    console.log(this.props);
    const { navigation } = this.props;
    const placeType = navigation.getParam("placeType");
    this.setState({ placeType: placeType });

    this.getCurrentLocation();
  }

/**
   * Get current user's position
   */
  getCurrentLocation() {
    navigator.geolocation.getCurrentPosition(position => {
      const lat = position.coords.latitude;
      const long = position.coords.longitude;
      this.setState({ lat: lat, long: long });
      this.getPlaces();
    });
  }

  /**
   * Get the Place URL
   */
  getPlacesUrl(lat, long, radius, type, apiKey) {
    const baseUrl = `https://maps.googleapis.com/maps/api/place/nearbysearch/json?`;
    const location = `location=${lat},${long}&radius=${radius}`;
    const typeData = `&types=${type}`;
    const api = `&key=${apiKey}`;
    return `${baseUrl}${location}${typeData}${api}`;
  }

  getPlaces() {
    const { lat, long, placeType } = this.state;
    const markers = [];
    const url = this.getPlacesUrl(lat, long, 1500, placeType, GOOGLE_API_KEY);
    fetch(url)
      .then(res => res.json())
      .then(res => {
        res.results.map((element, index) => {
          const marketObj = {};
          marketObj.id = element.id;
          marketObj.name = element.name;
          marketObj.photos = element.photos;
          marketObj.rating = element.rating;
          marketObj.vicinity = element.vicinity;
          marketObj.marker = {
            latitude: element.geometry.location.lat,
            longitude: element.geometry.location.lng
          };

          markers.push(marketObj);
        });
        //update our places array
        this.setState({ places: markers });
      });
  }

  render() {
    const { lat, long, places } = this.state;
    return (
      <View style={styles.container}>
        <View style={styles.mapView}>
          <MapView
            style={{
              flex: 1
            }}
            provider={PROVIDER_GOOGLE}
            initialRegion={{
              latitude: lat,
              longitude: long,
              latitudeDelta: 0.0922,
              longitudeDelta: 0.0421
            }}
          >
            {places.map((marker, i) => (
              <MapView.Marker
                key={i}
                coordinate={{
                  latitude: marker.marker.latitude,
                  longitude: marker.marker.longitude
                }}
                title={marker.name}
              />
            ))}
          </MapView>
react-native react-native-android react-native-ios react-native-maps
1个回答
0
投票

您是否尝试过react-native-geolocation-service

React native geolocation service for iOS and android.

为什么?

创建此库的目的是使用react-native的Geolocation API当前实现修复Android上的位置超时问题。该库尝试使用Google Play服务的新FusedLocationProviderClient API解决此问题,Google强烈推荐使用android的默认框架位置API。它会根据您的请求配置自动决定使用哪个提供程序,如果不满足您当前的请求配置,还会提示您更改位置模式。

注意:由于许多Android设备在硬件级别或系统软件级别都有GPS问题,因此位置请求仍然可能超时。检查常见问题以获取更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.