Как передать значения широты и долготы из UIViewController в MKMapView? - PullRequest
2 голосов
/ 19 апреля 2010

У меня есть подробный вид, который включает три кнопки UIB, каждый из которых выдвигает свой стек в стек. Одна из кнопок подключена к MKMapView. Когда эта кнопка нажата, мне нужно отправить переменные широты и долготы из детального вида в вид карты. Я пытаюсь добавить объявление строки в IBAction:

- (IBAction)goToMapView {

MapViewController *mapController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil]; 

mapController.mapAddress = self.address;
mapController.mapTitle = self.Title;

mapController.mapLat = self.lat;
mapController.mapLng = self.lng;

//Push the new view on the stack
[[self navigationController] pushViewController:mapController animated:YES];
[mapController release];
//mapController = nil;

}

И в моем файле MapViewController.h у меня есть:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "DetailViewController.h"
#import "CourseAnnotation.h"

@class CourseAnnotation;

@interface MapViewController : UIViewController <MKMapViewDelegate>
{
IBOutlet MKMapView *mapView;
NSString *mapAddress;
NSString *mapTitle;
NSNumber *mapLat;
NSNumber *mapLng;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) NSString *mapAddress;
@property (nonatomic, retain) NSString *mapTitle;
@property (nonatomic, retain) NSNumber *mapLat;
@property (nonatomic, retain) NSNumber *mapLng;

@end

И на соответствующих частях файла MapViewController.m у меня есть:

@synthesize mapView, mapAddress, mapTitle, mapLat, mapLng;

- (void)viewDidLoad 
{
    [super viewDidLoad];

[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };

region.center.latitude = mapLat; //40.105085;
region.center.longitude = mapLng; //-83.005237;

region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;  
[mapView setRegion:region animated:YES];

[mapView setDelegate:self];

CourseAnnotation *ann = [[CourseAnnotation alloc] init];
ann.title = mapTitle;
ann.subtitle = mapAddress;
ann.coordinate = region.center;
[mapView addAnnotation:ann];

}

Но я получаю это, когда пытаюсь построить: 'error: несовместимые типы в присваивании' для переменных lat и lng. Итак, мои вопросы: правильно ли я передаю переменные из одного представления в другое? И принимает ли MKMapView широту и долготу в виде строки или числа?

1 Ответ

6 голосов
/ 19 апреля 2010

Широта и долгота в MapKit хранятся как CLLocationDegrees типов, которые определены как double. Чтобы преобразовать ваши NSNumbers в double, используйте:

region.center.latitude = [mapLat doubleValue];

Или, возможно, лучше объявить ваши свойства как CLLocationDegrees с самого начала.

...