1

マップを特定の地域に集中させることに問題があります。どこに問題があるのか​​わからない

Map.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

Map.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 に答える