0

I am working on delivery related app. In this app, one view is showing map view. On which if map view change its region then how to find direction of map view's region from current location?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Maybe you can use CLLocationCoordinate2D and detect direction based on map center each time when didChangeRegion is invoked? – Mr. A Oct 28 '16 at 05:31

1 Answers1

0

You could create a property to store the last center coordinate and then find the direction between the new and last center coordinate, using the method from this answer https://stackoverflow.com/a/6140171/1757960

Something like this:

Create the property

@property (assign, nonatomic) CLLocationCoordinate2D lastCenterCoordinate;

Implement the delegate

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    float angle = [self angleFromCoordinate:self.lastCenterCoordinate toCoordinate:mapView.centerCoordinate];

    self.lastCenterCoordinate = mapView.centerCoordinate;
}

For reference, here's the method from the answer linked above

+ (float)angleFromCoordinate:(CLLocationCoordinate2D)first 
                toCoordinate:(CLLocationCoordinate2D)second 
{

    float deltaLongitude = second.longitude - first.longitude;
    float deltaLatitude = second.latitude - first.latitude;
    float angle = (M_PI * .5f) - atan(deltaLatitude / deltaLongitude);

    if (deltaLongitude > 0)      return angle;
    else if (deltaLongitude < 0) return angle + M_PI;
    else if (deltaLatitude < 0)  return M_PI;

    return 0.0f;
}

(note this was not tested)

Community
  • 1
  • 1
Jota
  • 149
  • 3
  • 9