Join Véronique Brossier for an in-depth discussion in this video Centering the map on the user's location, part of iOS App Development: Building Apps with MapKit and Core Location.
The mapView object is capable of displaying complex maps, but it also has much more functionality than that. It is set to communicate with your device GPS and therefore transfer live information and live data to your application. Let's demonstrate this in this application. Go to the XIB and open the Assistant Editor, hold the Ctrl key, make sure that you have the header file selected.
Hold the Ctrl key and drag and create an outlet which we are going to call mapView. Make sure to import the MapKit framework. Go back to the Standard Editor and add the MapViewDelegate. What the delegate does is it allows you to receive method calls from the object you set delegation on, in this case the mapView.
In this movie, we want to define a map region based on the user current location. So first, let's synthesize the mapView properly. Set the mapView delegate to a class, and because we want to access the user location, we have to define the ShowUserLocation property as YES. The method used to capture the user location is the following: mapView didUpdateUserLocation.
When the mapView receives the update from the device, it will call this method and parse the coordinate. So let's create a CLLocationCoordinate2D object in which we can store the userLocation. Next, let's create a region which will be defined by calling the method MKCoordinateRegionWithDistance, and this method takes three parameters.
The first one is the coordinate, the second one is going to be the spanDistance for the region along the latitude and longitude. Note that for this method we are parsing meters and not degrees. One meter represents about 3.3 feet, and now that the region is created, let's just set the region for the map to it, okay. Now let's run this. Note that for this movie, we are using the device. If you were to run this movie on your simulator, the user location need not be where you currently are but a default value.
So again to recap, to define a region, you could use hard coded data as well as live data.
Author Véronique Brossier covers the basic purpose of the Core Location and MapKit frameworks and how to integrate their services with your existing applications. Véronique shows how to center a map on a location, add annotations, create overlays, and zoom and navigate around maps. The course also covers how to leverage live location data such as course and speed and modify it when necessary.
- Displaying maps
- Reviewing the geographic coordinate system in MapKit
- Centering the map on the user location
- Adding interactivity to annotations
- Creating overlays with shapes and lines
- Starting and stopping location services
- Defining the distance between two locations
- Implementing forward or reverse geocoding