我试图在React Native中使用PubNub来实现实时位置跟踪,但由于我提到的代码已经4年了,因此PubNubReact已经不在那里了.我不知道如何使用当前可用的版本.为此,我使用了react-native map 和谷歌 map API.
以下是代码供参考:
import React from 'react';
import { StyleSheet, View, Platform, Dimensions, SafeAreaView } from 'react-native';
import MapView, {Marker, AnimatedRegion} from 'react-native-maps';
import PubNubReact from 'pubnub-react'
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;
export default class App extends React.Component{
constructor(props){
super(props);
this.state = {
latitude: LATITUDE,
longitude: LONGITUDE,
coordinate: new AnimatedRegion({
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: 0,
longitudeDelta: 0
})
}
this.pubnub = new PubNubReact({
publishKey: "pubKey",
subscribeKey: "subKey",
});
this.pubnub.init(this);
}
...