0

enter image description here

I am using MKMapView to show route. I want to zoom out this map so that it will show both source at destination at a time.

how can I zoom out map as per distance so that it will show whole route in one screen.

I am using this code:

double maxLatitude = annotation.coordinate.latitude;
double maxLongitude = annotation.coordinate.longitude;
double minLatitude = annotation.coordinate.latitude;
double minLongitude =  annotation.coordinate.longitude;
MKCoordinateRegion region;
region.center.latitude = (minLatitude + maxLatitude) / 2;
region.center.longitude = (minLongitude + maxLongitude) / 2;

region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING;

region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE)
? MINIMUM_VISIBLE_LATITUDE
: region.span.latitudeDelta;

region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING;


[self.mapView addAnnotation:annotation];

[self.mapView regionThatFits:region];
MPelletier
  • 16,256
  • 15
  • 86
  • 137
Pallavi
  • 249
  • 2
  • 10
  • 1
    http://stackoverflow.com/questions/5040175/iphone-mkmapview-set-map-region-to-show-all-pins-on-map – Ankur Jul 05 '13 at 10:18
  • In the code shown, the min and the max are set using the _same_ annotation. Also, regionThatFits _returns_ an MKCoordinateRegion (it doesn't actually change the map's region) so that line in the code actually does nothing. You need to call setRegion instead. Since it looks like you are drawing a polyline, you could just set the map's visibleMapRect to the MKPolyline's boundingMapRect. –  Jul 05 '13 at 12:34

1 Answers1

0

You can try it by the following method

 for (int i = 1; i<[arrayOfPointsToFindDistance count]; i++) {
            connection_listdata *detail_connection=[arrayOfPointsToFindDistance objectAtIndex:i];
            [self findDistancewithConnecionList:detail_connection];

 }

findDistancewithConnecionList method is given below.

-(void)findDistancewithConnecionList:(connection_listdata *)connectionlist{
NSLog(@"sssssss");

CLLocation *oldLoc = [[CLLocation alloc] initWithLatitude:connectionlist.coordinate_connection.latitude longitude: connectionlist.coordinate_connection.longitude];
for(int i =0; i<[mapArray count];i++){
    NSLog(@"ghsghshhs");
    NSMutableArray *firstArrayTemp = [mapArray objectAtIndex:i];
    connection_listdata *detail_connection=[firstArrayTemp objectAtIndex:0];
    CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:detail_connection.coordinate_connection.latitude longitude: detail_connection.coordinate_connection.longitude];
    CLLocationDistance d = [newLoc distanceFromLocation:oldLoc];

    NSLog(@"distanceeeee:%f",d);


    if(d <= 500){
        NSLog(@"foundddd");
        NSLog(@"maparray values= %@",mapArray);
        [firstArrayTemp addObject:connectionlist];
        [mapArray addObject:firstArrayTemp];
        return;

    }
[newLoc release];
}

NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:connectionlist];
[mapArray addObject:array];
[array release];
[oldLoc release];

return;

}