iphone - MKMapView 中的 zoomLevel 和 mapCenter

标签 iphone objective-c ios xcode mapkit

我正在使用 MKMapView 在 iPhone 中创建 map 应用程序.

我确实成功找到了我的 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];
}

我要解决的问题是:

我有一个 locationmap 上(此代码在 viewDidLoad 中):

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 map 中修复它?

最佳答案

创建位置数组,然后使用所有 AnnotationPins 创建中心 map View

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

之后使用这个数组使 map 居中

希望对你有帮助...

:)

关于iphone - MKMapView 中的 zoomLevel 和 mapCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12727520/

相关文章:

iphone - 使用 Core Data 存储位置数据

iphone - 通配符应用程序ID和捆绑ID的命名

ios - 在使用 "setImageWithURL"检索的 UIImageView 中重用 UIImage 对象

objective-c - 用于 Retina 显示的 URL 中的图像

ios - FBSDKAppInviteDialog 不工作

ios - 附属 Action 与选择转场

ios - react native ios 构建失败

iphone - 为什么 MKMapView 显示不正确的半径

iphone - 从 UITextField 输入动态调整 UITableView 大小

ios - 我可以在 semaphore.wait() 主线程上调用吗?