1

我正在使用MKMapView.

我确实成功地找到了我的current locationzoom在这一点上是这样的:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.mapView.delegate = self;

    self.locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    [self.mapView setShowsUserLocation:YES];
}

-(void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *annotationView = [views objectAtIndex:0];
    id<MKAnnotation> mp = [annotationView annotation];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1550, 1550);
    [self.mapView setRegion:region animated:YES];
}

我要解决的问题是:

我有一个(这个代码在location):mapviewDidLoad

CLLocationCoordinate2D location = [self getLocationFromAddressString:@"San Francisco, CA, United States"];
//    location.latitude = 37.78608;
//    location.longitude = -122.407398;

    MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Store location" coordinate:location];
    [self.mapView addAnnotation:mapAnnotation];

这是mapViewAnnotation:

@implementation MapViewAnnotation

@synthesize title = _title;
@synthesize coordinate = _coordinate;

- (id) initWithTitle:(NSString *) t coordinate:(CLLocationCoordinate2D) c {
    self = [super init];
    if(self) {
        _title = t;
        _coordinate = c;
    }
    return self;
}
@end

我想有一个适当的zoomLevelmapCenter为此location和我的current location.

我可以在 Android 中使用MapController.zoomToSpan(). 如何在 iPhone 地图中修复它?

4

2 回答 2

2

创建位置数组,然后使用所有 AnnotationPins 创建中心地图视图

-(void) RoutscenterMap 
{
        MKCoordinateRegion region;

        CLLocationDegrees maxLat = -90;
        CLLocationDegrees maxLon = -180;
        CLLocationDegrees minLat = 90;
        CLLocationDegrees minLon = 180;
        for(int idx = 0; idx < arrLocation.count; idx++)// here use your array or points
        {
            CLLocation* currentLocation = [arrLocation objectAtIndex:idx];
            if(currentLocation.coordinate.latitude > maxLat)
                maxLat = currentLocation.coordinate.latitude;
            if(currentLocation.coordinate.latitude < minLat)
                minLat = currentLocation.coordinate.latitude;
            if(currentLocation.coordinate.longitude > maxLon)
                maxLon = currentLocation.coordinate.longitude;
            if(currentLocation.coordinate.longitude < minLon)
                minLon = currentLocation.coordinate.longitude;
        }

        region.center.latitude     = (maxLat + minLat) / 2;
        region.center.longitude    = (maxLon + minLon) / 2;
        region.span.latitudeDelta  = (maxLat - minLat) * 2;
        region.span.longitudeDelta = (maxLon - minLon) * 2;

        [mapView setRegion:region animated:YES];
    }

您也可以在数组中添加位置,如下所示...

 CLLocation *temploc=[[CLLocation alloc]initWithLatitude:latitude longitude:longtitude];
 [arrLocation addObject:temploc];

之后使用此数组将地图居中

我希望这可以帮助你...

:)

于 2012-10-04T12:45:05.270 回答
0

这是我可以实现它的方式:

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

    CLLocationCoordinate2D bottomRightCoord;
    bottomRightCoord.latitude = 90;
    bottomRightCoord.longitude = -180;

    for(id<MKAnnotation> annotation in mapView.annotations) {
        topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
        topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
        bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
        bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.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;

    // Add a little extra space on the sides
    region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;

    region = [mapView regionThatFits:region];
    [mapView setRegion:region animated:YES];
于 2012-10-05T20:08:56.510 回答