0

Currently I am working on an app similar to Uber app for iOS devices.

For that purpose I used Google Map SDK

my query is: While driver drives he needs to see his current location with poly line on center of the map always, distance & total time travelled

Here I found one sample image with MKMapKit code:Image URL link and Sample code url

I am also looking same code for Google Map with custom image(Car symbol).

How can I solve this issue? please help me out.

shashank
  • 123
  • 14
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36

1 Answers1

1

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!

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59