ios - 释放放大后 map 会缩小

标签 ios objective-c mapkit apple-maps

我有一个 MapView,它在 map 上显示两个点和一条路线。当我放大 map 时,松开后, map 又缩小。

我在代码和 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

最佳答案

问题在于您通过调用 showAnnotations 设置 map 的可见区域。这与用户缩放发生冲突。

关于ios - 释放放大后 map 会缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47872688/

相关文章:

ios - 如何在 objective-c 中覆盖父类(super class)的私有(private)方法和属性

ios - 启用 UIScrollView 的缩放重置缩放 View 转换

objective-c - UITabBarController "want full screen"设置

ios - didSelect for MKAnnotationView 未触发

ios - React Native 用标记元素替换部分字符串

ios - iOS:如何正确获取电池电量

ios - 放大时使用 MKMapView 在 setUserTrackingMode 上崩溃

ios - 子类 MKTileOverlay 未显示在 map 上

ios - 如何对 NSFetchedResultsControllerDelegate 进行单元测试?

ios - navigationItem.titleView 中标题和副标题的字体大小没有改变