Я сделал счетчик шагов, подсчитываю шаги с помощью акселерометра iPhone, и теперь я хочу отобразить путь, который я сделал на mapView. Я могу легко показать карту и поймать правильную позицию с помощью iPhone GPS, я могу отображать начало и конечную точку, и я могу рисовать полилинию, чтобы показать путь, который я сделал. Теперь моя проблема в том, как установить правильный масштаб на карте? Когда я открываю ViewController с картой, это слишком далеко, и я должен увеличить масштаб, чтобы увидеть путь, который я сделал. Итак, мой вопрос в том, как установить правильный масштаб?Установите правильный масштаб изображения в MKMapView
я выкладываю здесь код View Controller с MAPview:
ResultViewController.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
Что случилось с этим кодом? Кто-нибудь может помочь мне правильно изменить карту?
Спасибо
«+1» приятно спасибо. –