Location and Maps

Article · 2:00

Foreground and background location, the two-step permission, and a map on the screen.

This lesson has 1 runnable example. Press Try yourself! under any of them to open it in the editor.

Location permission comes in two levels and they must be requested in order. When in use covers everything that happens while the reader is looking at your app, and is granted readily. Always, which allows updates when the app is closed, is a much bigger ask: on iOS you cannot request it until when-in-use has been granted, and even then the system may show a reminder later asking whether to keep allowing it. Ask for always up front and you will mostly be refused.

Accuracy is a budget. Accuracy.High uses GPS and drains the battery; Balanced uses wifi and cell towers and is enough to know which city, suburb or shop someone is in. Pick the lowest accuracy that answers your question, and prefer watchPositionAsync with a distance interval over a timer, so you get an update when the reader has actually moved.

Background location requires justification on both stores. Apple will ask what it is for during review and reject an app that cannot explain it, and both platforms show a persistent indicator while it is running.

For maps, react-native-maps renders Apple Maps on iOS and Google Maps on Android with one component. Markers are children, and the common performance mistake is rendering thousands of them - cluster instead.

TS Example 1 55 lines
import { useEffect, useState } from 'react';
import { View, Text } from 'react-native';
import * as Location from 'expo-location';
import MapView, { Marker, PROVIDER_GOOGLE } from 'react-native-maps';

export function NearbyMap() {
  const [coords, setCoords] = useState<Location.LocationObjectCoords | null>(null);
  const [denied, setDenied] = useState(false);

  useEffect(() => {
    let subscription: Location.LocationSubscription | undefined;

    (async () => {
      // Step one. "Always" cannot even be requested before this is granted.
      const { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setDenied(true);
        return;
      }

      // Balanced, not High: enough to know the neighbourhood, far cheaper.
      subscription = await Location.watchPositionAsync(
        {
          accuracy: Location.Accuracy.Balanced,
          distanceInterval: 50,      // metres moved, not seconds elapsed
        },
        (position) => setCoords(position.coords),
      );
    })();

    return () => subscription?.remove();
  }, []);

  if (denied) return <Text>We need your location to show what is nearby.</Text>;
  if (!coords) return <Text>Finding you...</Text>;

  return (
    <MapView
      style={{ flex: 1 }}
      provider={PROVIDER_GOOGLE}      // Google on both; omit for Apple on iOS
      initialRegion={{
        latitude: coords.latitude,
        longitude: coords.longitude,
        latitudeDelta: 0.02,
        longitudeDelta: 0.02,
      }}
      showsUserLocation
    >
      <Marker
        coordinate={{ latitude: coords.latitude, longitude: coords.longitude }}
        title="You are here"
      />
    </MapView>
  );
}

Open this example in the editor and change it. Nothing you type is saved.