如何将UIViewController中的纬度和经度值传递给MKMapView?

时间:2010-04-19 17:39:17

标签: iphone objective-c uiviewcontroller mkmapview mkcoordinateregion

我有一个包含三个UIButton的详细视图,每个UIButtons都会将不同的视图推送到堆栈。其中一个按钮连接到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];

}

但是当我尝试构建时,我得到了这个:'错误:分配中的不兼容类型'对于lat和lng变量。所以我的问题是我是否正确地将变量从一个视图传递到另一个视图? MKMapView是否接受纬度和经度作为字符串或数字?

1 个答案:

答案 0 :(得分:6)

MapKit中的纬度和经度存储为CLLocationDegrees类型,定义为double。要将NSNumbers转换为双精度数,请使用:

region.center.latitude = [mapLat doubleValue];

或者,或许更好,从一开始就将您的属性声明为CLLocationDegrees