-(Mkcoordinateregion) Regionforannotations: (Nsarray *) annotations//Can be copied using intact
{
Mkcoordinateregion region;
if ([annotations count] = = 0)
{
Region = Mkcoordinateregionmakewithdistance (self.mapView.userLocation.coordinate, 1000, 1000);
}
else if ([annotations count] = = 1)
{
ID <MKAnnotation> annotation = [annotations Lastobject];
Region = Mkcoordinateregionmakewithdistance (annotation.coordinate, 1000, 1000);
}
Else
{
Cllocationcoordinate2d Topleftcoord;
Topleftcoord.latitude =-90;
Topleftcoord.longitude = 180;
Cllocationcoordinate2d Bottomrightcoord;
Bottomrightcoord.latitude = 90;
Bottomrightcoord.longitude =-180;
For (id <MKAnnotation> annotation in annotations)
{
Topleftcoord.latitude = Fmax (Topleftcoord.latitude, annotation.coordinate.latitude);
Topleftcoord.longitude = Fmin (Topleftcoord.longitude, annotation.coordinate.longitude);
Bottomrightcoord.latitude = Fmin (Bottomrightcoord.latitude, annotation.coordinate.latitude);
Bottomrightcoord.longitude = Fmax (Bottomrightcoord.longitude, annotation.coordinate.longitude);
}
Const double EXTRASPACE = 1.1;
Region.center.latitude = Topleftcoord.latitude-(topleftcoord.latitude-bottomrightcoord.latitude)/2.0;
Region.center.longitude = Topleftcoord.longitude-(topleftcoord.longitude-bottomrightcoord.longitude)/2.0;
Region.span.latitudeDelta = Fabs (topleftcoord.latitude-bottomrightcoord.latitude) * EXTRASPACE;
Region.span.longitudeDelta = Fabs (topleftcoord.longitude-bottomrightcoord.longitude) * EXTRASPACE;
}
return [Self.mapview regionthatfits:region];
}
Instructions for use:
-(Ibaction) showlocations
{
Mkcoordinateregion region = [self regionforannotations:_locations];
Call the above method to get a mkcoordinateregion area that contains multiple tags in the _locations
[Self.mapview setregion:region Animated:yes];
}
Note: Objects in the _locations must implement protocol mkannotation and implement the following three methods:
-(cllocationcoordinate2d) coordinate
{
Return Cllocationcoordinate2dmake ([Self.latitude doublevalue], [Self.longitude doublevalue]);
}
-(NSString *) title
{
if ([self.locationdescription length] > 0)
{
return self.locationdescription;
}
Else
{
return @ "(No Description)";
}
}
-(NSString *) subtitle
{
return self.category;
}
iOS Development Learning Mapkit-get an area that displays multiple markers in Mapview (map) (mkcoordinateregion)