在放大后释放后映射缩放后退

时间:2017-12-18 16:35:26

标签: ios objective-c mapkit apple-maps

我有一个MapView,它在一个带有路线的地图上显示两个点。当我放大地图时,在我发布后,地图会缩小。

我在代码和 InterfaceBuilder

中将zoomEnabledscrollEnabled都设置为yes

enter image description here

@interface MapViewController () <UIApplicationDelegate, MKMapViewDelegate,CLLocationManagerDelegate> {
    CLLocationManager * locationManager;
    CLPlacemark * pmDesination;
    CLLocation * currentLocation;
    MyAnnotation * destinationAnn;
    MKPolyline *_routeOverlay;
    MKRoute *_currentRoute;
}

@end

@implementation MapViewController

const static int TYPE_STATUS_PICKUP = 0;
const static int TYPE_STATUS_DROPOFF = 1;

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self stopLocationServices];
}

- (void) viewDidLoad {
    [super viewDidLoad];


    self.mapView.delegate = self;
    self.mapView.zoomEnabled = YES;
    self.mapView.scrollEnabled = YES;
    [self startLocationServices];

    //Show points on map
    [self addressSearch:self.pickupLocation type:TYPE_STATUS_PICKUP];
    [self addressSearch:self.dropoffLocation type:TYPE_STATUS_DROPOFF];


}

- (void) mapViewDidFinishLoadingMap:(MKMapView *)mapView {

   [self showRoute];

    NSMutableArray * pins = [[NSMutableArray alloc] init];
    if (destinationAnn != nil) {
        [pins addObject:destinationAnn];
    }

    if ([self getCurrentLocationAnnotation] != nil) {
        [pins addObject:[self getCurrentLocationAnnotation]];
    }

    if (pins.count > 0) {
         [_mapView showAnnotations:pins animated:YES];
    }


}

#pragma mapping methods

- (void) addressSearch:(NSMutableDictionary *)pinLocation type:(int)type {

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:pinLocation[@"address"] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            if (error) {
               //ERROR LOOKING UP ADDRESS
            } else {


                CLPlacemark * pm = [placemarks lastObject];
                location.latitude = pm.location.coordinate.latitude;
                location.longitude = pm.location.coordinate.longitude;
                [ann setCoordinate:location];
                ann.title = [pinLocation objectForKey:@"title"];
                ann.subtitle = [pinLocation objectForKey:@"address"];

                if (type == _toLocation) {
                    destinationAnn = ann;
                }

                [self.mapView addAnnotation:ann];

             }

        }];

}

#pragma mark - SHOW ROUTE
- (void) showRoute {
    MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];

    MKMapItem *source = [MKMapItem mapItemForCurrentLocation];

    // Make the destination
    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(destinationAnn.coordinate.latitude, destinationAnn.coordinate.longitude);

    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];

    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];

    // Set the source and destination on the request
    [directionsRequest setSource:source];
    [directionsRequest setDestination:destination];

    MKDirections *directions = [[MKDirections alloc] initWithRequest:directionsRequest];

    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        if (error) {
            NSLog(@"There was an error getting your directions: %@", error.localizedDescription);
            return;
        }
        _currentRoute = [response.routes firstObject];
        [self plotRouteOnMap:_currentRoute];
    }];

}

- (void)plotRouteOnMap:(MKRoute *)route
{
    if(_routeOverlay) {
        [self.mapView removeOverlay:_routeOverlay];
    }

    // Update the ivar
    _routeOverlay = route.polyline;

    // Add it to the map
    [self.mapView addOverlay:_routeOverlay];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
    renderer.strokeColor = _toLocation ? [UIColor orangeColor] : [UIColor greenColor];
    renderer.lineWidth = 4.0;
    return  renderer;
}


// DELGATE THAT RUNS TO SHOW CURRENT USER LOCATION
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(nonnull NSError *)error {
    NSLog(@"Location Services Error: %@", [error description]);
    [[LoggingManager sharedReporting] addReportToLog:[NSString stringWithFormat:@"Mapping: locationManager:didFailWithError: %@",[error description] ]];
}

- (void) locationManager:(CLLocationManager *)manager didUpdateLocations: (NSArray *)locations {
    currentLocation = [locations lastObject];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 currentLocation = newLocation;
}


#pragma mark - GET TO AND CURRENT POINTS
- (NSString*) getDestination {

    NSDictionary * desDict = _toLocation ? self.dropoffLocation : self.pickupLocation;
    NSString * address = @"";

    if ([[desDict objectForKey:@"lat"] length] > 0 && [[desDict objectForKey:@"lon"] length] > 0) {
        address = [NSString stringWithFormat:@"%@,%@",[desDict objectForKey:@"lat"], [desDict objectForKey:@"lon"]];

    } else if ([desDict[@"address"] length] > 0 && [desDict[@"address"] rangeOfString:@"{"].location == NSNotFound) {
        address = [desDict objectForKey:@"address"];
    } else {
        address = @"NULL";
    }

    return address;

}

- (NSString*) getCurrentLocation {
    return [NSString stringWithFormat:@"%f,%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
}

- (MyAnnotation*) getCurrentLocationAnnotation {
    MyAnnotation * ann = [[MyAnnotation alloc] init];
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    [ann setCoordinate:location];
    ann.title = @"My Current Location";
    ann.subtitle = @"";
    return ann;
}
@end

1 个答案:

答案 0 :(得分:1)

问题是您通过调用showAnnotations来设置地图的可见区域。这与用户缩放相冲突。