-1

我制作了一个计步应用程序,我用 iPhone 加速度计计算步数,现在我想在 mapView 上显示我制作的路径。我可以轻松地显示地图并使用 iPhone GPS 捕捉正确的位置,我可以显示起点和终点,我可以绘制一条折线来显示我所做的路径。现在我的问题是如何在地图上设置正确的缩放比例?当我用地图打开 ViewController 时,它太远了,我必须缩放才能看到我制作的路径。所以我的问题是如何设置正确的缩放?

我在这里发布带有 mapView 的 View Controller 的代码:

结果视图控制器.m

#import "ResultViewController.h"
#import "MapAnnotation.h"

@interface ResultViewController ()
@property (nonatomic, strong)NSMutableArray *mapAnnotation;
@property (nonatomic) BOOL needUpdateRegion;
@end

@implementation ResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.labelResult.text = self.totalSteps;
    [self.mapView setDelegate:self];
    self.needUpdateRegion = YES;
    self.mapAnnotation = [[NSMutableArray alloc]initWithCapacity:2];
    CLLocation *startLocation = [self.locationArray objectAtIndex:0];
    CLLocation *stopLocation = [self.locationArray lastObject];
    NSString *startLatitude = [NSString stringWithFormat:@"%f", startLocation.coordinate.latitude];
    NSString *startLongitude = [NSString stringWithFormat:@"%f", startLocation.coordinate.longitude];
    NSString *stopLatitude = [NSString stringWithFormat:@"%f", stopLocation.coordinate.latitude];
    NSString *stopLongitude = [NSString stringWithFormat:@"%f", stopLocation.coordinate.longitude];

    MapAnnotation *startPlace = [[MapAnnotation alloc]initWithCoordinate:startLatitude andLong:startLongitude];
    MapAnnotation *stopPlace = [[MapAnnotation alloc]initWithCoordinate:stopLatitude andLong:stopLongitude];

    [self.mapAnnotation insertObject:startPlace atIndex:0];
    [self.mapAnnotation insertObject:stopPlace atIndex:1];

    if (self.mapAnnotation) {
        [self.mapView removeAnnotations:self.mapView.annotations];
    }

    [self.mapView addAnnotation:self.mapAnnotation[0]];
    [self.mapView addAnnotation:self.mapAnnotation[1]];
    [self updateRegion];
    [self createMKpolylineAnnotation];
}

- (void)updateRegion
{
    self.needUpdateRegion = NO;
    CGRect boundingRect;
    BOOL started = NO;
    for (id <MKAnnotation> annotation in self.mapView.annotations) {
        CGRect annotationRect = CGRectMake(annotation.coordinate.latitude, annotation.coordinate.longitude, 0, 0);
        if (!started) {
            started = YES;
            boundingRect = annotationRect;
        } else {
            boundingRect = CGRectUnion(boundingRect, annotationRect);
        }
    }
    if (started) {
        boundingRect = CGRectInset(boundingRect, -0.2, -0.2);
        if ((boundingRect.size.width < 20) && (boundingRect.size.height < 20)) {
            MKCoordinateRegion region;
            region.center.latitude = boundingRect.origin.x + boundingRect.size.width / 2;
            region.center.longitude = boundingRect.origin.y + boundingRect.size.height / 2;
            region.span.latitudeDelta = boundingRect.size.width;
            region.span.longitudeDelta = boundingRect.size.height;
            [self.mapView setRegion:region animated:YES];
        }
    }
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor blueColor];
    polylineView.lineWidth = 5.0;

    return polylineView;
}


- (void)createMKpolylineAnnotation {
    NSInteger numberOfSteps = self.locationArray.count;

    CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numberOfSteps);
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [self.locationArray objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;
        coords[index] = coordinate;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coords count:numberOfSteps];
    [self.mapView addOverlay:polyLine];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)restart:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

这段代码有什么问题?有人可以帮助我正确缩放地图吗?

谢谢

4

2 回答 2

2

在将 mapview 委托设置为后,首先在视图中添加这一行确实加载

[self.mapView setDelegate:self];
    self.needUpdateRegion = YES;
    mapView.zoomEnabled = YES;
    mapView.scrollEnabled = YES;

……

}

然后修改以下行以根据您的要求设置缩放级别 60000=60 km..

 [mapView setRegion: MKCoordinateRegionMakeWithDistance(site.coordinate, 60000, 60000)  animated:YES];

或者您可以尝试此方法将缩放比例设置为

    - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGImageRef imageReference = self.overlayImage.CGImage;

    MKMapRect theMapRect = self.overlay.boundingMapRect;
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);
    CGContextDrawImage(context, theRect, imageReference);
}

希望这可以帮助你..干杯!

于 2013-09-09T11:25:51.323 回答
2

在这种情况下,我建议您调用以下方法以缩放到地图的某个区域,您可以在方法的末尾添加调用- (void)createMKpolylineAnnotation;

称呼:

[self zoomToPolyLine: self.mapView polyLine:polyLine animated:YES];

方法:

-(void)zoomToPolyLine:(MKMapView*)map polyLine:(MKPolyline*)polyLine 
animated:(BOOL)animated
{
     MKPolygon* polygon = [MKPolygon polygonWithPoints:polyLine.points count:polyLine.pointCount];

    [map setRegion:MKCoordinateRegionForMapRect([polygon boundingMapRect]) 
     animated:animated];
}
于 2013-09-09T11:21:58.737 回答