问题
In this case when the app is initialize, the Pin-marker is set in the current position, but in the same map I have others markers, when I try to select other markers it fits again in the current position, how to solve this issue? I need to select any marker... and... When I find some place(with google places) it set a new marker and it set a new position and set the camera to the current search marker with zoom in this selected marker, and my other problem was make a function to get the current location(device user) by a button... someone can help me???
<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>
here is the button to call the current location:
<TouchableOpacity
onPress={this.getlocation}
style={styles.fab2}>
<Image
source={require('../assets/gps.png')}
style={{
width: 35,
height: 35,
margin: 10,
tintColor: 'white',
}}
/>
</TouchableOpacity>
回答1:
I got a solution to this problem, here is the snack to your read...
here is the simple code bellow:
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!'
);
}
);
};
and I call this function at my created button:
<TouchableOpacity
onPress={this.getLocationHandler}
style={styles.fab2}>
<Image
source={require('../assets/gps.png')}
style={{
width: 35,
height: 35,
margin: 10,
tintColor: '#FF3D00',
}}
/>
</TouchableOpacity>
Here is the link to the full code:
https://snack.expo.io/@matheus_cbrl/react-native-maps-autocomplete---address---current-location-button
Please if this answered your possible problem, like it!! :)
来源:https://stackoverflow.com/questions/58221741/how-to-fit-a-zoom-in-the-specific-marker-in-react-native-and-can-select-any-othe