Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a overlay marker on map which stays is center of the screen in react-native

hey i am trying to add overlay marker which stays in center of the screen like this even if we scroll or zoom the screen

enter image description here

i tried moving marker from its initial position but it lags behind when we move screen fast. so i want to set marker image in the center of the screen

here is the code what i have done to move marker:

componentDidMount: function() {
    this.fetchData();
    navigator.geolocation.getCurrentPosition(
      (position) => {
        this.setState({
          region: {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            latitudeDelta: LATITUDE_DELTA,
            longitudeDelta: LONGITUDE_DELTA
          }
        });
      },
    );
    this.watchID = navigator.geolocation.watchPosition((position) => {
      const newRegion = {
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
        latitudeDelta: LATITUDE_DELTA,
        longitudeDelta: LONGITUDE_DELTA
      }

      this.onRegionChange(newRegion);
    });

  },

  componentWillUnmount: function() {
    navigator.geolocation.clearWatch(this.watchID);
  },

  onRegionChange(region) {
    this.setState({ region });
  },

so how can i overlay a marker image?

like image 747
atif Avatar asked Jul 21 '16 09:07

atif


People also ask

How do you place marker when click anywhere on the map view in react native?

Adding a marker in React Native MapsStart by importing Marker from react-native-maps . import { Marker } from "react-native-maps"; Next, render the <Marker /> component as a child of <MapView /> . Pass the coordinate for the marker in the coordinate prop.

How do you use fitToSuppliedMarkers in react native maps?

The react-native-maps has a method called fitToSuppliedMarkers for this specific reason. To implement this you need to populate the identifier props of the Marker, and then call the fitToSuppliedMarkers method of the MapView using ref, when you have received your list of coordinates.


1 Answers

I see two options to solve this issue:

The MapView way

Just add a MapView.Marker with the position of your region variable:

  <MapView
    style={styles.map}
    region={this.state.region}
    onRegionChange={this.onRegionChange.bind(this)}>
    <MapView.Marker
      coordinate={{latitude: this.state.region.latitude, longitude: this.state.region.longitude}}
    />
  </MapView>

In my opinion this is the "correct" way, though I face the issue, that the Marker updates its position with a small delay: As I move the map, it stays on its position in respect to the map and a after a few hundred milliseconds it jumps back to the center of the map (suggestions on how to debug or fix this issue are very welcome).

The second option overcomes this issue

The Icon way

Put your MapView inside a View, that fills the whole space. Inside the View add a centered Icon and your map:

<View style={StyleSheet.absoluteFillObject}>
  <Icon name="map-marker" 
    style={{    
      zIndex: 3,
      position: 'absolute',
      marginTop: -37,
      marginLeft: -11,
      left: '50%',
      top: '50%'}} 
    size={40}
    color="#f00" />
  <MapView
    style={StyleSheet.absoluteFillObject}
    region={this.state.region}
    onRegionChange={this.onRegionChange.bind(this)}>
  </MapView>
</View>

Note the use of zIndex to overlay it in front of the MapView and marginTop/marginLeft which can be used to place the needletip of the marker on the center.

like image 74
moritzschaefer Avatar answered Oct 13 '22 04:10

moritzschaefer