set the region for all annotation

- (void)updateMemberPins
{
    [map addAnnotations:locationArray];
    
    if ([locationArray count] > 0) {
        CLLocationCoordinate2D topLeftCoord;
        topLeftCoord.latitude = -90;
        topLeftCoord.longitude = 180;
        
        CLLocationCoordinate2D bottomRightCoord;
        bottomRightCoord.latitude = 90;
        bottomRightCoord.longitude = -180;
        
        for (UserAnnotation *item in locationArray) {
            topLeftCoord.longitude = fmin(topLeftCoord.longitude, item.localCoordinate.longitude);
            topLeftCoord.latitude = fmax(topLeftCoord.latitude, item.localCoordinate.latitude);
            bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, item.localCoordinate.longitude);
            bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, item.localCoordinate.latitude);
        }
        
        MKCoordinateRegion region;
        region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
        region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
        region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
        region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
        
        region = [map regionThatFits:region];
        [map setRegion:region animated:YES];
    }
}

UserAnnotation为自定义的Annotation,拓展了一些功能。

实现的功能:用一个最小的圆,囊括了所有显示在地图上的点。

reference:http://stackoverflow.com/questions/1336370/positioning-mkmapview-to-show-multiple-annotations-at-once

http://stackoverflow.com/questions/5040175/iphone-mkmapview-set-map-region-to-show-all-pins-on-map

posted @ 2012-06-18 18:12  Lawerence  阅读(193)  评论(0编辑  收藏  举报