import React from 'react';
import { StyleSheet, View, Dimensions, Platform, 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;
console.disableYellowBox = true;
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
latitude: LATITUDE,
longitude: LONGITUDE,
coordinate: new AnimatedRegion({
latitude: null,
longitude: null,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}),
};
// Replace "X" with your PubNub Keys
this.pubnub = new PubNubReact({
publishKey: 'X',
subscribeKey: 'X',
});
this.pubnub.init(this);
}
// code to receive messages sent in a channel
componentDidMount() {
this.subscribeToPubNub();
}
subscribeToPubNub = () => {
this.pubnub.subscribe({
channels: ['location'],
withPresence: true,
});
this.pubnub.getMessage('location', msg => {
const { coordinate } = this.state;
const { latitude, longitude } = msg.message;
const newCoordinate = { latitude, longitude };
if (Platform.OS === 'android') {
if (this.marker) {
this.marker._component.animateMarkerToCoordinate(newCoordinate, 500);
}
} else {
coordinate.timing(newCoordinate).start();
}
this.setState({
latitude,
longitude,
});
});
};
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
ref={c => (this.mapView = c)}
region={this.state.latitude ? this.getMapRegion() : null}
>
<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,
},
});
export default App;
我在React Native代码中遇到错误**“无法读取属性”animateMarkerToCoordinate“of undefined”**。我正在Android上开发位置跟踪应用程序。该应用程序从另一个应用程序中提取LAT和LONG,并希望在GoogleMap上显示动画。我在其他一些网站上看到animateMarkerToCoordinate在React Native for Android中不工作。这是真的吗?如果不是,该问题的解决方案是什么?
5条答案
按热度按时间fgw7neuy1#
此问题出现在react native
0.63.+
版本中。根据react-native-maps
的官方文档但这是不工作与最新的React原生版本。只是删除
_component
之前animateMarkerToCoordinate
它会工作得很好。smtd7mpg2#
在当前版本的React Native和PubNub(4.31+)中,使用
getMessage
方法可能无法正确获取消息,因此“消息”显示为undefined
。因此,如果有人遇到这个问题,只需使用
addListener
而不是getMessage
,并遵循其各自的文档。我已经花了几天的时间试图运行同样的代码,我希望我能帮助别人。
bwitn5fc3#
基于GitHub的回答:https://github.com/react-native-community/react-native-maps/issues/2195#issuecomment-424246731,最好只使用
coordinate.timing(newCoordinate).start();
代码。导致此故障的原因是
newCoordinate
缺少latitudeDelta
和longitudeDelta
,因此最终代码应如下所示:pobjuy324#
使用try catch。它不会崩溃
bfnvny8b5#
此.标记.动画标记到坐标使用此而不是此.标记._组件.动画标记到坐标