iOS Development Learning Mapkit-get an area that displays multiple markers in Mapview (map) (mkcoordinateregion)

Source: Internet
Author: User

-(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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.