CodeBlanks Logo Icon CodeBlanks

Summary - Location

Combine expo-location, getCurrentPositionAsync and watchPositionAsync to write a location tracking app.

Summary - Location

In this group you learned to install the expo-location package, ask for location permission, get the current location with getCurrentPositionAsync, track continuously with watchPositionAsync and convert an address to a coordinate with geocodeAsync. The core of a location app is these five pieces.

In the summary we will merge permission, current location and address resolution in a single app. These three pieces together form a real location flow. Let us merge them now.

Practice Summary - Get the current location

Complete the getLocation function. Call const loc = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High }). Return an object with return { latitude: loc.coords.latitude, longitude: loc.coords.longitude }.

Starter code

App.js
import * as Location from 'expo-location';

export async function getLocation() {
  // get the current location and return lat/lng
}

Sample solution

App.js
const loc = await Location.getCurrentPositionAsync({
  accuracy: Location.Accuracy.High,
});
return { latitude: loc.coords.latitude, longitude: loc.coords.longitude };

Practice Summary - Address to coordinate

Complete the geocodeAddress function. Call const results = await Location.geocodeAsync(address). Return the first result with return results[0].

Starter code

App.js
import * as Location from 'expo-location';

export async function geocodeAddress(address) {
  // convert the address to a coordinate and return the first result
}

Sample solution

App.js
const results = await Location.geocodeAsync(address);
return results[0];

Questions and answers

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

1 Write the permission function

Question
const permission = await Location.____();
Answer
const permission = await Location.requestForegroundPermissionsAsync();

Explanation: This function asks for location permission.

2 Match each flow part to its role

Term Meaning
requestForegroundPermissionsAsync Location permission
getCurrentPositionAsync Current location
watchPositionAsync Continuous tracking
geocodeAsync Address to coordinate

All lessons in Advanced