1

我在将地图居中到特定区域时遇到问题。我不知道问题出在哪里

地图.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface Map : UIViewController {
    MKMapView * mapView;
}

@property(nonatomic,retain) IBOutlet MKMapView * mapView;

- (IBAction)setMap:(id)sender;

- (IBAction)getLocation:(id)sender;


@end

地图.m

#import "Map.h"
#import "Annotation.h"


@interface Map ()

@end

//define coordinates

#define Sofia_LATITUDE 42.745826;
#define Sofia_LONGITUDE 23.270186;

#define Plovdiv_LATITUDE 42.1333;
#define Plovdiv_LONGITUDE 24.75;

#define Varna_LATITUDE 43.2;
#define Varna_LONGITUDE 27.9167;

#define THE_SPAN 3.8;

@implementation Map

@synthesize mapView;

- (IBAction)getLocation:(id)sender {
    mapView.showsUserLocation = YES;
    [self.view addSubview:mapView];

}

- (IBAction)setMap:(id)sender {
    switch (((UISegmentedControl *)sender).selectedSegmentIndex) {
        case 0:
            mapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapView.mapType = MKMapTypeHybrid;
            break;
        default:
            break;

    }
}

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

- (void)viewDidLoad
{

    [super viewDidLoad];


    MKCoordinateRegion myRegion;

    //Center the map



    CLLocationCoordinate2D center;
    center.latitude = 42.1333;
    center.longitude = 24.75;

    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;

    [mapView setRegion:myRegion animated:TRUE];

    NSMutableArray * locations = [[NSMutableArray alloc]init];
    CLLocationCoordinate2D  location;
    Annotation * myAnn;

    //make annotations

    myAnn = [[Annotation alloc]init];
    location.latitude = Sofia_LATITUDE;
    location.longitude = Sofia_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"Милото";
    myAnn.subtitle = @"Моята любов е ТУК!!!";
    [locations addObject:myAnn];

    myAnn = [[Annotation alloc]init];
    location.latitude = Plovdiv_LATITUDE;
    location.longitude = Plovdiv_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"Plovdiv";
    myAnn.subtitle = @"Plovdiv";
    [locations addObject:myAnn];

    myAnn = [[Annotation alloc]init];
    location.latitude = Varna_LATITUDE;
    location.longitude = Varna_LONGITUDE;
    myAnn.coordinate = location;
    myAnn.title = @"Varna";
    myAnn.subtitle = @"Varna";
    [locations addObject:myAnn];


    [self.mapView addAnnotations:locations];


}

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

@end

注释.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface Annotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString * title;
@property (nonatomic, copy) NSString * subtitle;

@end

注释.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle;

@end

因此,当我启动程序并单击按钮查看地图时,它会出现但未居中。当我向后移动并再次单击以查看地图时,一切正常。问题出在哪里?

4

1 回答 1

1

您需要为地图视图设置区域。该区域有一个坐标(中间)和一个跨度(跨度的数字代表地图上显示的世界的度数),使用跨度数字来获得您想要的结果。您可以在设置坐标时设置区域。

MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
region.span = span;
region.center = self.coordinate;
[mapView setRegion:region animated:YES];
于 2013-04-15T10:58:40.050 回答