react-native-maps – React Native Mapview component for iOS + Android

On a React Native project I’m currently working on, I’m implementing native (vector) mapviews. Gladly there’s react-native-maps by AirBnB to handle it all.

import React, { Component } from 'react';
import MapView from 'react-native-maps';

class MapViewExample extends Component {

  constructor(props) {
    super(props);
    this.state = {
      region: {
        latitude: 37.78825,
        longitude: -122.4324,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
      },
      markers: [
      ],
    };
  }

  onRegionChange = (region) => {
    this.setState({ region });
  }

  onMapPress = (e) => {
    this.setState({
      markers: [
        ...this.state.markers,
        {
          coordinate: e.nativeEvent.coordinate,
          key: `marker_${this.state.markers.length}`,
        },
      ],
    });
  }

  render() {
    return (
      <MapView
        region={this.state.region}
        onRegionChange={this.onRegionChange}
        onPress={this.onMapPress}
      >
        {this.state.markers.map(marker => (
          <MapView.Marker
            coordinate={marker.coordinate}
            key={marker.key}
          />
        ))}
      </MapView>
    );
  }

}

By default uses Mapkit on iOS, and Google Maps on Android. It’s also possible to use Google Maps on iOS.

react-native-maps

Elsewhere , , , ,

2 Responses to react-native-maps – React Native Mapview component for iOS + Android

  1. Pingback: XCode 9.0: Fixing a Slow/Unresponsive iOS Simulator | Bram.us

  2. Pingback: react-native-maps-directions – Directions/Routing component for react-native-maps | Bram.us

Leave a Reply

Your email address will not be published. Required fields are marked *