Question

I have mapView that suppose to contain both user location pin and object pin. What i want, is show screen that contain both pins, no matter how large distance is between them (it suppose to be not too much). For some reason i can't show user location, screen moves to some point in the ocean. There is my code and attempts to get it:

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


}

There is might be "too much" code, but i can't figure out where i made mistake i paste it all. I think i might have problem with array containing location objects.

How to finally make screen with that two locations? Any advice would be appreciated, thanks!

Était-ce utile?

La solution

Try creating an area flyTo that will "contain" your given annotations:

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top