ios - 需要显示显示两个 CLLocation 引脚的屏幕

标签 ios objective-c mkmapview core-location

我的 mapView 假设包含用户位置图钉和对象图钉。我想要的是包含两个引脚的显示屏幕,无论它们之间的距离有多大(应该不会太大)。出于某种原因,我无法显示用户位置,屏幕移动到海洋中的某个点。这是我的代码并尝试获取它:

-(MKCoordinateRegion)regionForAnnotations:(NSArray*)annotations{


    MKCoordinateRegion region;

    if ([annotations count] ==0){
        region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 1000, 1000);
    }   else if ([annotations count] ==1 ){
        id <MKAnnotation> annotation = [annotations lastObject];
        region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000);
    }   else {
        CLLocationCoordinate2D topLeftCord;
        topLeftCord.latitude = -90;
        topLeftCord.longitude = 180;

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

        for (id <MKAnnotation> annotation in annotations){
            topLeftCord.latitude = fmax(topLeftCord.latitude, annotation.coordinate.latitude);
            topLeftCord.longitude = fmin(topLeftCord.longitude, annotation.coordinate.longitude);

            bottomRightCord.latitude = fmin(bottomRightCord.latitude, annotation.coordinate.latitude);
            bottomRightCord.longitude = fmax(bottomRightCord.longitude, annotation.coordinate.longitude);


        }

        const double extraSpace = 1.1;
        region.center.longitude = topLeftCord.longitude - (topLeftCord.longitude - bottomRightCord.longitude)/2.0;
        region.center.latitude = topLeftCord.latitude - (topLeftCord.latitude - bottomRightCord.latitude)/2.0;

        region.span.latitudeDelta = fabs(topLeftCord.latitude - bottomRightCord.latitude)*extraSpace;
        region.span.longitudeDelta = fabs(topLeftCord.longitude - bottomRightCord.longitude)*extraSpace;


    }

    return [self.mapView regionThatFits:region];

}

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    //2
    static NSString *identifier = @"Location";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView == nil){
        annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
    }

    //3
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;
    annotationView.pinColor = MKPinAnnotationColorPurple;

    //4
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:@selector(getRoute:) forControlEvents:UIControlEventTouchUpInside];
    annotationView.rightCalloutAccessoryView = rightButton;

    return annotationView;
};


-(void)setEntityCoordiate{

    CLLocationCoordinate2D location;
    location.latitude = [self.latitudeString doubleValue];
    location.longitude = [self.longitudeString doubleValue];

    CLLocationCoordinate2D user;
    user.latitude = self.mapView.userLocation.coordinate.latitude;
    user.latitude = self.mapView.userLocation.coordinate.longitude;

    CLLocation *userLocation = [[CLLocation alloc]initWithLatitude:user.latitude longitude:user.longitude ];
    CLLocation *entityLocation = [[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];

    MKCoordinateSpan span;
    span.latitudeDelta = 100;
    span.longitudeDelta = 100;

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 400, 400);

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

    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
    annotation.coordinate = location;
    annotation.title = _titleForEntity;

    [_locations addObject:userLocation];
    [_locations addObject:entityLocation];
    [_mapView addAnnotation:annotation];
}

-(void)getRoute:(UIButton*)button{

    MKCoordinateRegion region = [self regionForAnnotations:_locations];
    [self.mapView setRegion:region animated:YES];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setEntityCoordiate];


}

可能有“太多”的代码,但我无法弄清楚我在哪里犯了错误,我把它全部粘贴了。我想我可能对包含位置对象的数组有疑问。

如何最终用这两个位置制作屏幕?任何建议将不胜感激,谢谢!

最佳答案

尝试创建一个 flyTo 区域,它将“包含”您给定的注释:

MKMapRect flyTo = MKMapRectNull;

for (id <MKAnnotation> annotation in annotations) {
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(flyTo)) {
        flyTo = pointRect;
    } else {
        flyTo = MKMapRectUnion(flyTo, pointRect);
    }
}

mapview.visibleMapRect = flyTo;

关于ios - 需要显示显示两个 CLLocation 引脚的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22529346/

相关文章:

ios - 找不到 Apple 推送通知服务的证书签名请求 (CSR)

ios - iCloud 和核心数据不工作 - URLForUbiquityContainerIndentifier 是零

objective-c - iPhone/iPad : Unable to copy folder from NSBundle to NSDocumentDirectory

ios - 在 iOS 上存储身份验证 token - NSUserDefaults 与钥匙串(keychain)?

swift - 如何让文本字段悬停/ float 在 mapView 上方

objective-c - MKMapView setVisibleMapRect edgePadding 在动画时更改相机标题

ios - 在 JWTCryptoSecurity 类中找不到方法 keyFromPemFileWithName

ios - 在 Objective-C 中使用带或不带后缀 .f 的 CGFloat 是否存在性能差异

ios - Cocoapods: 没有这样的模块 'CorePlot'

ios 6 - 允许用户在街上创建 map 注释的应用程序返回具有相同坐标的多个注释