2012-10-04 22 views
1

我在iPhone上使用MKMapView创建一个地图应用程序。个zoomLevel和mapCenter中的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]; 
} 

,我想解决的问题是:

我对map(此代码locationviewDidLoad ):

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 

我想为这个location和我current location适当zoomLevelmapCenter

我可以使用MapController.zoomToSpan()做成功的Android。我如何在iPhone Map中修复它?

回答

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]; 
2

创建与所有AnnotationPins位置该中心的MapView和后阵列

-(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]; 

使用后此数组居中地图

我希望这会帮助你...

:)

+0

它是某种硬编码。你有在Android中使用MapController.zoomToSpan()吗?事情是我想中心,放大我的当前位置和'CLLocationCoordinate2D位置',你的代码不帮我。 – Ali

+0

我有什么'didAddAnnotationViews'方法?我应该在哪里使用你的方法? – Ali

+0

哎交配其很容易只看到如果创建CLLocationCoordinate2D对象阵列存储你们两个位置,并在arrLocation添加此并调用LocationUpdate方法或viewWillAppear中的方法此方法..只是尝试使用它.. –