我正在使用以下代码,但没有成功:
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]}
/>
//组件内部类的DidMount
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。
现在 animateToCoordinate 已被弃用,如果你想做同样的事情,你应该使用 animateCamera 或 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>
)
}
我遇到了一些未定义 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 引用更新为更新的方式。参见: 引用示例
像下面这样使用了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>
)
}
这是我的解决方案,因为上面提供的解决方案对我不起作用:
import MapView from 'react-native-maps';
import React, { useRef } from "react";
const defaultRegion = {
latitude: 59.9139,
longitude: 10.7522,
latitudeDelta: 0.0922/1,
longitudeDelta: 0.0521/1
}
const newLocation = {
latitude: 2.0469,
longitude: 45.3182
}
const MapScreen = () => {
const mapViewRef = useRef(null);
return (
<View style={styles.mapContainer}>
<MapView ref={ mapViewRef } style={styles.map} initialRegion = { defaultRegion } >
...
</MapView>
<TouchableOpacity style={styles.button} onPress = {() => mapViewRef.current.animateToRegion( newLocation, 1000)} >
<Text>Move Location</Text>
</TouchableOpacity>
</View>
);
};
主要收获:
1. const mapViewRef = useRef(null);
2. ref={ mapViewRef }
3. onPress = {() => mapViewRef.current.animateToRegion( newLocation, 1000)}
这是一个解决方案。
const animateToCoordinat = (lat, long) => {
mapView?.animateCamera({
center: {
latitude: lat,
longitude: long,
},
duration: {
1000,
}
});
}
https://github.com/react-native-maps/react-native-maps/blob/master/docs/mapview.md#:~:text=current%20camera%20configuration.-,animateCamera,-camera%3A%20Camera%2C%20%7B%20duration
您也可以使用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 });
}
我希望这也能帮助你,这真的是一个很好的组件! 并且非常有用,祝贺相关人员。