放大后地图缩小

Posted

技术标签:

【中文标题】放大后地图缩小【英文标题】:Map Zooms Back Out after releasing on Zoom-in 【发布时间】:2017-12-18 16:35:26 【问题描述】:

我有 MapView,它在带有路线的地图上显示两个点。当我放大地图时,释放后,地图又缩小了。

我在代码和 InterfaceBuilder

中将 zoomEnabledscrollEnabled 都设置为 yes

@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】:

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

【讨论】:

啊,明白了。非常感谢!

以上是关于放大后地图缩小的主要内容,如果未能解决你的问题,请参考以下文章

您好,我在arcmap中地图放大或缩小后 就无法 全图显示 就找不到地图了 怎么办??谢谢

Maphilight() 在放大/缩小图像地图后停止正常工作

带有集群和自定义视图标记的谷歌地图在放大和缩小时滞后太多

放大和缩小 d3 地图时如何查询数据集?

ArcGIS API for JavaScript做地图的放大缩小

mapkit 缩小然后放大