React-Native-Maps:如何动画协调?
React-Native-Maps: How to animate to coordinate?
我使用以下代码没有成功:
//NativeBase容器内部
<MapView.Animated
ref="map"
style = {styles.map}
showsUserLocation = {true}
zoomEnabled = {true}
showsMyLocationButton = {true}
showsCompass = {true}
showScale = {true}
showsIndoors = {true}
mapType = {this.state.mapTypes[this.state.mapTypeCode]}
/>
//componentDidMount里面的class
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position.coords);
this.setState({position: initialPosition});
let tempCoords = {
latitude: Number(position.coords.latitude),
longitude: Number(position.coords.longitude)
}
this.refs.map.animateToCoordinate(tempCoords, 1);
}, function (error) { alert(error) },
);
但是报错说没有animateToCoordinate这个函数
我遇到了一些 this.refs 未定义的问题,除非我在构造函数中将对此的引用绑定到我在 this.refs 中使用的函数。在你的情况下,试试这个:
constructor(props) {
super(props);
this._getCoords = this._getCoords.bind(this);
this.state = {
position: null
};
}
componentDidMount() {
this._getCoords();
}
_getCoords = () => {
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position.coords);
this.setState({position: initialPosition});
let tempCoords = {
latitude: Number(position.coords.latitude),
longitude: Number(position.coords.longitude)
}
this._map.animateToCoordinate(tempCoords, 1);
}, function (error) { alert(error) },
);
};
render() {
return (
<MapView.Animated
ref={component => this._map = component}
/>
);
}
尽管仍然可以使用字符串引用,但我相信现在它已经过时了,所以我也将 MapView 引用更新为更新的方式。参见:Ref Example
在我的例子中,我使用了 animateToCoordinate(),如下所示,它对我有用:
var _mapView: MapView;
render() {
return (
<MapView style = {styles.maps}
ref = {(mapView) => { _mapView = mapView; }}
initialRegion = {{
latitude: 6.8523,
longitude: 79.8895,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
<TouchableOpacity
onPress = {() => _mapView.animateToCoordinate({
latitude: LATITUDE,
longitude: LONGITUDE
}, 1000)}>
<Text>Tap</Text>
</TouchableOpacity>
)
}
不幸的是,现在不推荐使用 animateToCoordinate,如果您想这样做,您应该改用 animateCamera or animateToRegion。
render() {
return (
<MapView style = {styles.maps}
ref = {(mapView) => { _mapView = mapView; }}
initialRegion = {{
latitude: 6.8523,
longitude: 79.8895,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
<TouchableOpacity
onPress = {() => _mapView.animateToRegion({
latitude: LATITUDE,
longitude: LONGITUDE
}, 1000)}>
<Text>Tap</Text>
</TouchableOpacity>
)
}
这是一个解决方案。
const animateToCoordinat = (lat, long) => {
mapView?.animateCamera({
center: {
latitude: lat,
longitude: long,
},
duration: {
1000,
}
});
}
您也可以使用 setCamera
方法。
我的案例使用了动画相机功能,我使用了 ,这个组件生成了一个很酷的动画...
<MapView.Animated
provider = { Platform.OS === 'android' ? PROVIDER_GOOGLE : PROVIDER_DEFAULT } // remove if not using Google Maps
style = { styles_l.map }
ref = {(map) => (this.map = map)}
initialRegion = { this.state.region }
followsUserLocation = { true}
showsCompass = { true}
showsUserLocation = { true}
onRegionChange = {() => this.handleChangeRegion }
onRegionChangeComplete = {() => this.handleChangeRegion.bind(this) }>
</MapView.Animated >
这个组件会调用这个函数:
handleChangeRegion = (region) => {
this.map.animateCamera({ center: region });
}
希望对你也有帮助,真是一个不错的组件!非常有用,祝贺所有相关人员。
我使用以下代码没有成功:
//NativeBase容器内部
<MapView.Animated
ref="map"
style = {styles.map}
showsUserLocation = {true}
zoomEnabled = {true}
showsMyLocationButton = {true}
showsCompass = {true}
showScale = {true}
showsIndoors = {true}
mapType = {this.state.mapTypes[this.state.mapTypeCode]}
/>
//componentDidMount里面的class
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position.coords);
this.setState({position: initialPosition});
let tempCoords = {
latitude: Number(position.coords.latitude),
longitude: Number(position.coords.longitude)
}
this.refs.map.animateToCoordinate(tempCoords, 1);
}, function (error) { alert(error) },
);
但是报错说没有animateToCoordinate这个函数
我遇到了一些 this.refs 未定义的问题,除非我在构造函数中将对此的引用绑定到我在 this.refs 中使用的函数。在你的情况下,试试这个:
constructor(props) {
super(props);
this._getCoords = this._getCoords.bind(this);
this.state = {
position: null
};
}
componentDidMount() {
this._getCoords();
}
_getCoords = () => {
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position.coords);
this.setState({position: initialPosition});
let tempCoords = {
latitude: Number(position.coords.latitude),
longitude: Number(position.coords.longitude)
}
this._map.animateToCoordinate(tempCoords, 1);
}, function (error) { alert(error) },
);
};
render() {
return (
<MapView.Animated
ref={component => this._map = component}
/>
);
}
尽管仍然可以使用字符串引用,但我相信现在它已经过时了,所以我也将 MapView 引用更新为更新的方式。参见:Ref Example
在我的例子中,我使用了 animateToCoordinate(),如下所示,它对我有用:
var _mapView: MapView;
render() {
return (
<MapView style = {styles.maps}
ref = {(mapView) => { _mapView = mapView; }}
initialRegion = {{
latitude: 6.8523,
longitude: 79.8895,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
<TouchableOpacity
onPress = {() => _mapView.animateToCoordinate({
latitude: LATITUDE,
longitude: LONGITUDE
}, 1000)}>
<Text>Tap</Text>
</TouchableOpacity>
)
}
不幸的是,现在不推荐使用 animateToCoordinate,如果您想这样做,您应该改用 animateCamera or animateToRegion。
render() {
return (
<MapView style = {styles.maps}
ref = {(mapView) => { _mapView = mapView; }}
initialRegion = {{
latitude: 6.8523,
longitude: 79.8895,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
/>
<TouchableOpacity
onPress = {() => _mapView.animateToRegion({
latitude: LATITUDE,
longitude: LONGITUDE
}, 1000)}>
<Text>Tap</Text>
</TouchableOpacity>
)
}
这是一个解决方案。
const animateToCoordinat = (lat, long) => {
mapView?.animateCamera({
center: {
latitude: lat,
longitude: long,
},
duration: {
1000,
}
});
}
您也可以使用 setCamera
方法。
我的案例使用了动画相机功能,我使用了
<MapView.Animated
provider = { Platform.OS === 'android' ? PROVIDER_GOOGLE : PROVIDER_DEFAULT } // remove if not using Google Maps
style = { styles_l.map }
ref = {(map) => (this.map = map)}
initialRegion = { this.state.region }
followsUserLocation = { true}
showsCompass = { true}
showsUserLocation = { true}
onRegionChange = {() => this.handleChangeRegion }
onRegionChangeComplete = {() => this.handleChangeRegion.bind(this) }>
</MapView.Animated >
这个组件会调用这个函数:
handleChangeRegion = (region) => {
this.map.animateCamera({ center: region });
}
希望对你也有帮助,真是一个不错的组件!非常有用,祝贺所有相关人员。