I am Developing sample react-native Application, in that application i am using react-native-maps. It's Working fine maps showing current Location, But ,I have Custom latitude Longitude,now I want to navigate to that particular location when i click a Button.
Here This is My Code:
<MapView
ref='map'
style={styles.map}
region={this.state.mapRegion}
showsUserLocation={true}
followUserLocation={true}
zoomEnabled={true}
pitchEnabled={true}
showsCompass={true}
showsBuildings={true}
showsTraffic={true}
showsIndoors={true}
onRegionChange={this.onRegionChange.bind(this)}
onPress={this.onMapPress.bind(this)}>
{this.state.markers.length != 0 || this.state.markers != undefined ?(
this.state.markers.map(marker => (
<MapView.Marker
coordinate={{
latitude: marker.Latitude||this.state.lastLat||region.latitude,
longitude:marker.Longitude||this.state.lastLong||region.longitude
}}
image = {
marker.EmergencyService == true ?(
require('./Imgs/emergencyService.png')):
(require('./Imgs/regularService.png'))
}
onCalloutPress={() => this.bookDoctorsAppointment(marker)}
>
<MyCustomMarkerView marker={marker} navigator={this.props.navigator}/>
</MapView.Marker>
)
)):(<View></View>)}
</MapView>
It seems there are a bunch of methods available, I try animateToRegion below:
https://github.com/airbnb/react-native-maps/blob/master/docs/mapview.md#methods
So try using that ref:
class MyComp extends Component {
setMap = () => {
if (this.map) {
console.log('keys on this.map:', Object.keys(this.map));
this.map.animateToRegion(...);
}
}
getRefMap = el => this.map = el; // this also triggers when <MapView unmounts but el will be `null` so you can test if (el) { alert('mounted') } else { alert('unmounted') }
render() {
return (
<View>
<Text onPress={this.setMap}>Set map!</Text>
<MapView ref={this.getRefMap} ...>;
</View>
)
}
}
Related
I want to display overlay component when pressing the map marker. When pressing the button, overlay is showing fine. But when pressing the marker, it is not showing. Can someone let me know about how to display overlay when pressing the map marker.
Here is the code.
const toggleOverlay = () => {
setVisible(!visible);
};
return(
<View style={styles.container}>
<MapView style={styles.map}
showsUserLocation={true}
followUserLocation={true}
zoomEnabled={true}
showsMyLocationButton={true}
initialRegion={ mapRegion}
provider = {PROVIDER_GOOGLE}
showsTraffic={true}
>
{
locationPoints? locationPoints.map((point) => (
<Marker
coordinate={{
longitude: parseFloat(point.PointLongitude),
latitude: parseFloat(point.PointLatitude)
}}
title= {point.name}
pinColor={'blue'}
**onPress={() => (toggleOverlay)}**
>
</Marker>
))
: null}
</MapView>
<Button title="Confirm Address"
**onPress={toggleOverlay}** />
<Overlay isVisible={visible} onBackdropPress={toggleOverlay}>
<Text>Your Address</Text>
<View>
<Text>Address</Text>
</View>
</Overlay>
</View>
);
try, maybe it works)
{
locationPoints? locationPoints.map((point) => (
<Marker
coordinate={{
longitude: parseFloat(point.PointLongitude),
latitude: parseFloat(point.PointLatitude)
}}
title= {point.name}
pinColor={'blue'}
onPress={toggleOverlay}
/>
))
: null
}
MapBox with react-native.
How to know if a point is on "water"(ocean) or land.
can't find function visibleFeatures in react-native?
var LocationSelect = function(props) {
const mapContainer = useRef(null);
function onMapPointSelect(data) {
/// Get if point is water or land
mapContainer.visibleFeatures(data.geometry.coordinates, styleLayerIdentifiers: ["water"]);
}
return (
<View style={styles.mapContainer}>
<MapboxGL.MapView style={styles.map}
onLayout={onLayout}
onPress={onMapPointSelect}
ref={el => (mapContainer.current = el)} >
</MapboxGL.MapView>
</View>
);
}
Thank you very much
I'm currently learning react-native and react-native-maps (using typescript ES6) and I'm having some issues rendering markers in that they just don't appear. I've attempted to separate my logic into components that make's sense to me but I'm wondering if this is the reason I can't get them to render.
What I have is the following:
Main Map Component
<MapView style={styles.map}
region={this.props.region}
showsUserLocation={true}
followsUserLocation={false}>
<View>
<SearchCircle />
<MarkerList />
<View>
<MapView>
SearchCircle
The SearchCircle works perfectly and the code contains:
import { Circle } from 'react-native-maps';
...
class SearchCircle extends React.Component<SearchProps> {
render() {
return (
<Circle center={this.props.user.location}
radius={this.props.map.searchRadius}
fillColor={'rgba(230,238,255,0.5)'}
strokeColor={'#1A66FF'}
strokeWidth={1} />
);
}
}
MarkerList - ISSUE
The MarkerList is the component that does not render correctly. It contains:
class MarkerList extends React.Component<MarkerListProps> {
// render our contacts
render() {
return (
<View>
{this.props.markerlist[0] != undefined && this.props.markerList.map((marker, index) => (
this.renderMarker( marker, index )
))}
</View>
);
}
renderMarker( marker: MarkerEntry, index: number ) {
return (
<View key={index}>
<Marker coordinate={marker.location} title={marker.title} />
<Text>{'Lat: ' + marker.location.latitude + ', Long: ' + marker.location.longitude}</Text>
</View>
);
}
}
The MarkerList component does call render() when the state updates (when I update the markers locations) BUT only the Text element renders/updates correctly. The Lat/Lng is displayed correctly on my screen however the Marker element does not appear at all. The markerlist property for the MarkerList component is a list of type MarkerEntry which contains the following:
interface MarkerEntry {
title: string
location: LatLng
}
No matter what I try, I cannot get any markers to render from this array. If I statically define markers then everything works just fine but with my property array list map they never render.
Static Example that works
class MarkerList extends React.Component<MarkerListProps> {
// render our contacts
render() {
let marker = {
id: 'Test',
location: {
latitude: -31.970097415447232,
longitude: 115.92362245895334
}
};
let markerArray: any[] = [];
markerArray.push( marker );
return (
<View>
{markerArray[0] != undefined && markerArray.map((c, index) => (
<Marker key={index} coordinate={c.location} title={c.id} />
))}
</View>
);
}
}
Is anyone able to assist. I can't understand what I'm doing wrong or if I'm missing something about how react-native should be structured for this to work.
Thanks in advance!
So I've managed to solve my problem and thought I'd leave my final solution here for anyone else that needs it. The issues were with how I was defining the MapView and also with how I was returning the map results.
Map Component
For the main map component I had to remove the child <View> entry I had defined.
Before
<MapView style={styles.map}
region={this.props.region}
showsUserLocation={true}
followsUserLocation={false}>
<View>**
<SearchCircle />
<MarkerList />
<View>
<MapView>
After
<MapView style={styles.map}
region={this.props.region}
showsUserLocation={true}
followsUserLocation={false}>
<SearchCircle />
<MarkerList />
<MapView>
MarkerList
The second part I had to change was the render function and it's syntax in the render function.
Before
class MarkerList extends React.Component<MarkerListProps> {
// render our contacts
render() {
return (
<View>
{this.props.markerlist[0] != undefined && this.props.markerList.map((marker, index) => (
this.renderMarker( marker, index )
))}
</View>
);
}
renderMarker( marker: MarkerEntry, index: number ) {
return (
<View key={index}>
<Marker coordinate={marker.location} title={marker.title} />
<Text>{'Lat: ' + marker.location.latitude + ', Long: ' + marker.location.longitude}</Text>
</View>
);
}
}
After
Note the additional return statements outside and inside the map call and the removal of the <View> element.
class MarkerList extends React.Component<MarkerListProps> {
// render our contacts
render() {
return this.props.markerList.map((marker, index) => {
return( this.renderMarker( marker, index ));
});
}
renderMarker( marker: MarkerEntry index: number) {
return (
<View key={index}>
<Marker coordinate={marker.location} title={marker.title} />
</View>
);
}
}
I'm having trouble displaying my <MapView /> inside a Tab. I'm using react-native-scrollable-tab-view
The <MapView /> won't display in the map even after using : ...StyleSheet.absoluteFillObject. I can display the map only if I put a fix height. For example: height: 500.
Here is what my sample tab screen looks like.
styles = StyleSheet.create({
tabContent: {
flex: 1,
},
map: {
...StyleSheet.absoluteFillObject,
},
});
class HomeTemporary extends Component {
render() {
return (
<ScrollableTabView
renderTabBar={() => <ScrollableTabBar />}
>
<ScrollView tabLabel="List" style={styles.tabContent}>
<Text>Content Tab One</Text>
</ScrollView>
<ScrollView tabLabel="Map" style={styles.tabContent}>
<MapView
scrollEnabled
style={styles.map}
provider={MapView.PROVIDER_GOOGLE}
initialRegion={{
latitude: 25.2048493,
longitude: 55.2707828,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}}
/>
</ScrollView>
</ScrollableTabView>
);
}
}
Absolutely about what? If the map inside the view, the view component must also specify the absolute value of the width and height. In your case for tabContent.
Custom annotation on mapbox
How can I achieve this using Mapbox and Annotations in React Native. I have tried nesting the annotations, rendering as polyline but am not getting the desired result. Can anyone help with resolving this?
Check out this working code. Finally was able to figure out the correct way to do it :
Inside my render() :
<Mapbox.MapView
ref={map => { this.map = map; }}
styleURL={Mapbox.StyleURL.Basic}
zoomLevel={15}
centerCoordinate={[11.256, 43.770]}
style={{flex: 1}}
showUserLocation={true}
userTrackingMode={Mapbox.UserTrackingModes.Follow}>
{this.state.markers.map(marker => (
<Mapbox.PointAnnotation
key= {marker.title}
id= {marker.title}
coordinate={marker.coordinates}>
<View style={styles.annotationContainer}>
<View style={styles.annotationFill} />
</View>
<Mapbox.Callout title={marker.title} />
</Mapbox.PointAnnotation>
))}
</Mapbox.MapView>
Function to update this.state.markers :
_getAnnotations = (key, location) => {
let newelement = {
title: key,
coordinates: location,
};
this.setState(prevState => ({
markers: [...prevState.markers, newelement]
}))
}
Geoquery trigger :
this.state.geoQuery.on("key_entered", (key, location, distance) => {
this._getAnnotations(key,location);
});