gpt4 book ai didi

react-native - 如何使用 react-native-maps 中的自定义按钮转到我当前的位置?

转载 作者:行者123 更新时间:2023-12-03 23:11:01 25 4
gpt4 key购买 nike

我在 android 上使用 react-native-maps。我放了一个自定义按钮,单击它应该转到提供的坐标(我当前的位置)

<MapView
ref={(map) => { this.map = map; }}
provider={PROVIDER_GOOGLE}
style={{ height: '100%', width: '100%' }}
annotations={markers}
showsUserLocation={true}
showsMyLocationButton={true}
initialRegion={this.state.region}
onRegionChangeComplete={this.onRegionChange}
>

我的按钮 -
<TouchableOpacity onPress={this.gotToMyLocation} style={[Style.alignJustifyCenter, {
width: 60, height: 60,
position: "absolute", bottom: 20, right: 20, borderRadius: 30, backgroundColor: "#d2d2d2"
}]}>
<Image
style={{ width: 40, height: 40 }}
source={Images.myLocation}
/>
</TouchableOpacity>

gotToMyLocation 方法 -
gotToMyLocation(){
console.log('gotToMyLocation is called')
navigator.geolocation.getCurrentPosition(
({ coords }) => {
console.log("curent location: ", coords)
console.log(this.map);
if (this.map) {
console.log("curent location: ", coords)
this.map.animateToRegion({
latitude: coords.latitude,
longitude: coords.longitude,
latitudeDelta: 0.005,
longitudeDelta: 0.005
})
}
},
(error) => alert('Error: Are location services on?'),
{ enableHighAccuracy: true }
)
}

console.log(this.map) 总是显示未定义。

单击按钮时,我想将 map 移动到当前位置。

最佳答案

实际上,我发现了一个愚蠢的错误,即绑定(bind)该方法会使它起作用。可能对某人有用。
this.gotToMyLocation = this.gotToMyLocation.bind(this);

关于react-native - 如何使用 react-native-maps 中的自定义按钮转到我当前的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com