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



我有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;

最新更新