If you're the driver, you can use Geolocation to detect your current location.
Based from this thread, you have to retrieve the position by using CLLocationManager
.
First, add the CoreLocation.framework
to your project :
- Go in
Project Navigator
- Select your project
- Click on the tab
Build Phases
- Add the
CoreLocation.framework
in the Link Binary with Libraries
Then all you need to do is to follow the basic example of Apple
documentation.
Create a CLLocationManager
probably in your ViewDidLoad
:
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500; // meters
[locationManager startUpdatingLocation];
With the CLLocationManagerDelegate
every time the position is
updated, you can update the user position on your Google Maps
:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
// Update your marker on your map using location.coordinate.latitude
//and location.coordinate.longitude);
}
}
It will also work if you use the native MapKit.framework
. You need to add yourMapView.myLocationEnabled = YES;
and the framework will do everything. (Except center the map on you position).
Follow the steps from this documentation. If you want to update the map to follow your position, you can copy Google example MyLocationViewController.m
that is included in the framework directory. They just add a observer on the myLocation
property to update the camera properties.
To get the total distance travelled, check this related question. It states that you need to geocode the address to latitude/longitude, then you can use the CLLocation
framework to calculate the distance.
To geocode the adress, you could use this forward geocoding
API.
// get CLLocation fot both addresses
CLLocation *location = [[CLLocation alloc] initWithLatitude:address.latitude longitude:address.longitude];
// calculate distance between them
CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation];
You can also check this related link.
Hope this helps!