MapKit:如何平移和缩放以适合我的所有注释?

发布于 2024-09-11 02:03:15 字数 69 浏览 5 评论 0原文

所以我在我的地图上散布了一堆注释......一切都很漂亮。现在我需要能够设置地图的位置和缩放,以便它们完美契合。我该怎么做?

So I've got a bunch of annotations spread out on my map... everything is just dandy. Now I need to be able to set the map's position and zoom so they all fit perfectly. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-09-18 02:03:15

来自苹果论坛...

- (void)recenterMap {

         NSArray *coordinates = [self.mapView valueForKeyPath:@"annotations.coordinate"];



         CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};

         CLLocationCoordinate2D minCoord = {90.0f, 180.0f};



         for(NSValue *value in coordinates) {

              CLLocationCoordinate2D coord = {0.0f, 0.0f};

              [value getValue:&coord];

              if(coord.longitude > maxCoord.longitude) {

                   maxCoord.longitude = coord.longitude;

              }

              if(coord.latitude > maxCoord.latitude) {

                   maxCoord.latitude = coord.latitude;

              }

              if(coord.longitude < minCoord.longitude) {

                   minCoord.longitude = coord.longitude;

              }

              if(coord.latitude < minCoord.latitude) {

                   minCoord.latitude = coord.latitude;

              }

         }

         MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};

         region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;

         region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;

         region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;

         region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;

         [self.mapView setRegion:region animated:YES];  

    }

From Apple forums...

- (void)recenterMap {

         NSArray *coordinates = [self.mapView valueForKeyPath:@"annotations.coordinate"];



         CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};

         CLLocationCoordinate2D minCoord = {90.0f, 180.0f};



         for(NSValue *value in coordinates) {

              CLLocationCoordinate2D coord = {0.0f, 0.0f};

              [value getValue:&coord];

              if(coord.longitude > maxCoord.longitude) {

                   maxCoord.longitude = coord.longitude;

              }

              if(coord.latitude > maxCoord.latitude) {

                   maxCoord.latitude = coord.latitude;

              }

              if(coord.longitude < minCoord.longitude) {

                   minCoord.longitude = coord.longitude;

              }

              if(coord.latitude < minCoord.latitude) {

                   minCoord.latitude = coord.latitude;

              }

         }

         MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};

         region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;

         region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;

         region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;

         region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;

         [self.mapView setRegion:region animated:YES];  

    }
┼── 2024-09-18 02:03:15
@interface MKMapView (zoomToFit)

- (void)zoomToFitOverlays; //Animation defaults to YES
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay;
- (void)zoomToFitOverlays:(NSArray *)someOverlays;

- (void)zoomToFitOverlaysAnimated:(BOOL)animated;
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay animated:(BOOL)animated;
- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated;

- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated insetProportion:(CGFloat)insetProportion; //inset 0->1, defaults in other methods to .1 (10%)

@end

@implementation MKMapView (zoomToFit)


#pragma mark -
#pragma mark Zoom to Fit

- (void)zoomToFitOverlays {
    [self zoomToFitOverlaysAnimated:YES];
}

- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay {
    [self zoomToFitOverlay:[NSArray arrayWithObject:anOverlay] animated:YES];
}

- (void)zoomToFitOverlays:(NSArray *)someOverlays {
    [self zoomToFitOverlays:someOverlays animated:YES];
}

- (void)zoomToFitOverlaysAnimated:(BOOL)animated {
    [self zoomToFitOverlays:self.overlays animated:animated];
}

- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay animated:(BOOL)animated {
    [self zoomToFitOverlays:[NSArray arrayWithObject:anOverlay] animated:YES];  
}

- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated {
    [self zoomToFitOverlays:someOverlays animated:animated insetProportion:.1];
}

- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated insetProportion:(CGFloat)insetProportion {
    //Check
    if ( !someOverlays || !someOverlays.count ) {
        return;
    }

    //Union
    MKMapRect mapRect = MKMapRectNull;
    if ( someOverlays.count == 1 ) {
        mapRect = ((id<MKOverlay>)someOverlays.lastObject).boundingMapRect;
    } else {
        for ( id<MKOverlay> anOverlay in someOverlays ) {
            mapRect = MKMapRectUnion(mapRect, anOverlay.boundingMapRect);
        }
    }

    //Inset
    CGFloat inset = (CGFloat)(mapRect.size.width*insetProportion);
    mapRect = [self mapRectThatFits:MKMapRectInset(mapRect, inset, inset)];

    //Set
    MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);
    [self setRegion:region animated:animated];
}

@end
@interface MKMapView (zoomToFit)

- (void)zoomToFitOverlays; //Animation defaults to YES
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay;
- (void)zoomToFitOverlays:(NSArray *)someOverlays;

- (void)zoomToFitOverlaysAnimated:(BOOL)animated;
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay animated:(BOOL)animated;
- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated;

- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated insetProportion:(CGFloat)insetProportion; //inset 0->1, defaults in other methods to .1 (10%)

@end

@implementation MKMapView (zoomToFit)


#pragma mark -
#pragma mark Zoom to Fit

- (void)zoomToFitOverlays {
    [self zoomToFitOverlaysAnimated:YES];
}

- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay {
    [self zoomToFitOverlay:[NSArray arrayWithObject:anOverlay] animated:YES];
}

- (void)zoomToFitOverlays:(NSArray *)someOverlays {
    [self zoomToFitOverlays:someOverlays animated:YES];
}

- (void)zoomToFitOverlaysAnimated:(BOOL)animated {
    [self zoomToFitOverlays:self.overlays animated:animated];
}

- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay animated:(BOOL)animated {
    [self zoomToFitOverlays:[NSArray arrayWithObject:anOverlay] animated:YES];  
}

- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated {
    [self zoomToFitOverlays:someOverlays animated:animated insetProportion:.1];
}

- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated insetProportion:(CGFloat)insetProportion {
    //Check
    if ( !someOverlays || !someOverlays.count ) {
        return;
    }

    //Union
    MKMapRect mapRect = MKMapRectNull;
    if ( someOverlays.count == 1 ) {
        mapRect = ((id<MKOverlay>)someOverlays.lastObject).boundingMapRect;
    } else {
        for ( id<MKOverlay> anOverlay in someOverlays ) {
            mapRect = MKMapRectUnion(mapRect, anOverlay.boundingMapRect);
        }
    }

    //Inset
    CGFloat inset = (CGFloat)(mapRect.size.width*insetProportion);
    mapRect = [self mapRectThatFits:MKMapRectInset(mapRect, inset, inset)];

    //Set
    MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);
    [self setRegion:region animated:animated];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文