在这种情况下,当应用程序初始化时,Pin-marker设置在当前位置,但是在同一张地图中,我还有其他标记,当我尝试选择其他标记时,它又适合当前位置,如何解决这个问题?我需要选择任何标记...,然后...当我找到某个地方(带有Google地方)时,它设置了一个新标记,并设置了一个新位置,并将相机缩放到当前搜索标记,并放大了所选标记,而我的另一个问题是通过按钮来获取当前位置(设备用户)的功能...有人可以帮助我???
<View style={styles.container}>
<StatusBar hidden />
<MapView
onPress={this.handleMapPress}
style={StyleSheet.absoluteFill}
ref={map => (this.mapView = map)}
rotateEnabled={true}
scrollEnabled={true}
showsMyLocationButton={true}
followsUserLocation={true}
showsUserLocation={true}
zoomEnabled={true}
showsPointsOfInterest={true}
showBuildings={false}
//region={this.props.region}
initialRegion={region}
provider="google">
{!!location && (
<MapView.Marker
coordinate={location}
onPress={this.handleMarkerPress}>
<Image
source={isAddressVisible ? placholder2 : placholder}
style={styles.icon}
/>
</MapView.Marker>
)}
{this.state.coordinates.map(
(coordinates, index, title, description, location) => (
<MapView.Marker
onPress={this.handleMapPress}
ref={mark => (coordinates.mark = mark)}
key={`coordinate_${index}`}
title={coordinates.title}
description={coordinates.description}
coordinate={{
latitude: coordinates.latitude,
longitude: coordinates.longitude,
}}>
<Image
source={isAddressVisible ? placholder2 : placholder}
style={styles.icon}
/>
</MapView.Marker>
)
)}
<MapView.Marker coordinate={this.props.region} >
<Image
source={ markerImage}
style={styles.icon}
/>
</MapView.Marker>
</MapView>
这里是呼叫当前位置的按钮:
<TouchableOpacity
onPress={this.getlocation}
style={styles.fab2}>
<Image
source={require('../assets/gps.png')}
style={{
width: 35,
height: 35,
margin: 10,
tintColor: 'white',
}}
/>
</TouchableOpacity>
我已经解决了这个问题,这是您阅读的点心...
这是下面的简单代码:
pickLocationHandler = event => {
const coords = event.nativeEvent.coordinate;
this.map.animateToRegion({
...this.state.focusedlocation,
latitude: coords.latitude,
longitude: coords.longitude,
});
this.setState(prevState => {
return {
focusedlocation: {
...prevState.focusedlocation,
latitude: coords.latitude,
longitude: coords.longitude,
},
locationChosen: true,
};
});
};
getLocationHandler = () => {
navigator.geolocation.getCurrentPosition(
pos => {
const coordsEvent = {
nativeEvent: {
coordinate: {
latitude: pos.coords.latitude,
longitude: pos.coords.longitude,
},
},
};
this.pickLocationHandler(coordsEvent);
},
err => {
console.log(err);
alert(
'Ocorreu um erro ao carregar sua localização, por favor ligue o GPS!'
);
}
);
};
并且我在创建的按钮上调用此函数:
<TouchableOpacity
onPress={this.getLocationHandler}
style={styles.fab2}>
<Image
source={require('../assets/gps.png')}
style={{
width: 35,
height: 35,
margin: 10,
tintColor: '#FF3D00',
}}
/>
</TouchableOpacity>
这里是完整代码的链接:
如果这回答了您可能的问题,请喜欢它!! :)