【问题标题】:I need to get my current location using GPS programmatically in react native How can i achieve it?我需要使用 GPS 以编程方式获取我当前的位置,以响应原生我如何实现它?
【发布时间】:2020-08-01 20:29:12
【问题描述】:

我正在使用 react 原生地图和地理位置。我已将纬度和经度设置为

const LATITUDE = 37.78825;
const LONGITUDE = -122.4324

当我的应用程序启动并调用 this.watchlocation 函数时。纬度和经度更改为我当前的位置,因此 componentDidupdate 起作用并将消息发送到 pubnub ..但是当我走一段距离时。组件没有更新没有调用..我想走多远才能再次向 PubNub 发送消息。我正在使用 expo ~37.0.3 和 "react-native-maps": "0.27.1"

  • 这是执行此操作的示例代码

MAP.js

import MapView, { Marker, AnimatedRegion } from 'react-native-maps';

import PubNub from "pubnub";
const { width, height } = Dimensions.get('window');

const ASPECT_RATIO = width / height;
const LATITUDE = 37.78825;
const LONGITUDE = -122.4324;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;

const pubnub = new PubNub({
  subscribeKey: "demo",
  publishKey: "demo",
  uuid: "myUUIDv"
});

export default class Trackee extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      latitude: LATITUDE,
      longitude: LONGITUDE,
      coordinate: new AnimatedRegion({
        latitude: LATITUDE,
        longitude: LONGITUDE,
        latitudeDelta: 0,
        longitudeDelta: 0,
      }),
    };



  }

  componentDidMount() {
    this.watchLocation();
  }

  componentDidUpdate(prevProps, prevState) {



    if (this.state.latitude !== prevState.latitude) {
      pubnub.publish({
        message: {
          latitude: this.state.latitude,
          longitude: this.state.longitude,
        },
        channel: 'location',
      });
    }
  } 

  componentWillUnmount() {
    navigator.geolocation.clearWatch(this.watchID);
  }

  watchLocation = () => {
    const { coordinate } = this.state;

    this.watchID = navigator.geolocation.watchPosition(
      position => {
        const { latitude, longitude } = position.coords;

        const newCoordinate = {
          latitude,
          longitude,
        };

        if (Platform.OS === 'android') {
          if (this.marker) {
            coordinate.timing(newCoordinate).start(); // 500 is the duration to animate the marker
          }
        } else {
          coordinate.timing(newCoordinate).start();
        }

        this.setState({
          latitude,
          longitude,
        });
      },
      error => console.log(error),
      {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 1000,
        distanceFilter: 0,
      }
    );
  };

  getMapRegion = () => ({
    latitude: this.state.latitude,
    longitude: this.state.longitude,
    latitudeDelta: LATITUDE_DELTA,
    longitudeDelta: LONGITUDE_DELTA,
  });



  render() {
    return (
      <SafeAreaView style={{ flex: 1 }}>
        <View style={styles.container}>
          <MapView style={styles.map} showUserLocation followUserLocation loadingEnabled region={this.getMapRegion()}>
            <Marker.Animated
              ref={marker => {
                this.marker = marker;
              }}
              coordinate={this.state.coordinate}

            />



        </MapView>

        </View>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    ...StyleSheet.absoluteFillObject,
    justifyContent: 'flex-end',
    alignItems: 'center',
  },
  map: {
    ...StyleSheet.absoluteFillObject,
  },
});

【问题讨论】:

    标签: react-native geolocation react-native-maps


    【解决方案1】:

    https://www.npmjs.com/package/react-native-get-location

    你可以尝试使用这个包来动态获取当前纬度

    【讨论】:

    • 我可以在 expo 中使用它吗?在那里我可以允许访问位置等权限
    • 我必须跟踪骑手..所以我需要在每次骑手改变位置时获取位置
    • 如果您的目标是实现跟踪系统,那么您可以尝试使用 Google 的 API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多