CodeBlanks Logo Icon CodeBlanks

Summary - Maps

Combine MapView, Marker and onRegionChange to write a map app.

Summary - Maps

In this group you learned to install the react-native-maps package, show a map with MapView, drop markers with Marker, track region changes with onRegionChange and create custom markers. The core of a map app is these five pieces.

In the summary we will merge the map, marker and region tracking in a single app. These three pieces together form a real map screen. Let us merge them now.

Practice Summary - Drop a marker at Istanbul

Complete the empty App component. Open <MapView style={{{ flex: 1 }}} initialRegion={{{ latitude: 41.0082, longitude: 28.9784, latitudeDelta: 0.05, longitudeDelta: 0.05 }}}>. Inside add <Marker coordinate={{{ latitude: 41.0082, longitude: 28.9784 }}} title="Istanbul" />. Close MapView.

Starter code

App.js
import { MapView, Marker } from 'react-native-maps';

export default function App() {
  // drop a marker at Istanbul
  return null;
}

Sample solution

App.js
return (
  <MapView
    style={{ flex: 1 }}
    initialRegion={{
      latitude: 41.0082,
      longitude: 28.9784,
      latitudeDelta: 0.05,
      longitudeDelta: 0.05,
    }}
  >
    <Marker
      coordinate={{ latitude: 41.0082, longitude: 28.9784 }}
      title="Istanbul"
    />
  </MapView>
);

Practice Summary - Track region change

Complete the empty App component. Return <MapView style={{{ flex: 1 }}} initialRegion={{{ latitude: 41.0082, longitude: 28.9784, latitudeDelta: 0.05, longitudeDelta: 0.05 }}} onRegionChange={(region) => console.log(region.latitude)} />.

Starter code

App.js
import { MapView } from 'react-native-maps';

export default function App() {
  // track region change and log the latitude
  return null;
}

Sample solution

App.js
return (
  <MapView
    style={{ flex: 1 }}
    initialRegion={{
      latitude: 41.0082,
      longitude: 28.9784,
      latitudeDelta: 0.05,
      longitudeDelta: 0.05,
    }}
    onRegionChange={(region) => console.log(region.latitude)}
  />
);

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Write the initialRegion prop

Question
<MapView style={{ flex: 1 }} ____={{ latitude: 41.0, longitude: 28.9, latitudeDelta: 0.05, longitudeDelta: 0.05 }} />
Answer
<MapView style={{ flex: 1 }} initialRegion={{ latitude: 41.0, longitude: 28.9, latitudeDelta: 0.05, longitudeDelta: 0.05 }} />

Explanation: initialRegion sets the opening position.

2 Match each flow part to its role

Term Meaning
MapView Map view
Marker Drops marker
onRegionChange Tracks region
initialRegion Opening position

All lessons in Advanced