Die*_*ado 6 javascript google-maps react-native react-redux react-native-maps
我使用以下代码但没有成功:
//在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]}
/>
Run Code Online (Sandbox Code Playgroud)
//在类的componentDidMount内部
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) },
);
Run Code Online (Sandbox Code Playgroud)
但是发生了一个错误,说没有这样的函数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}
/>
);
}
Run Code Online (Sandbox Code Playgroud)
虽然仍然可以使用字符串引用,但我相信这是遗留的,所以我也将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>
)
}
Run Code Online (Sandbox Code Playgroud)
小智 6
不幸的是,现在 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>
)
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
12243 次 |
最近记录: |