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

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Join the Conversation

2 Comments

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.