我有一个包含三个UIButton的详细视图,每个UIButton将一个不同的视图压入堆栈。按钮之一连接到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是否接受纬度和经度作为字符串或数字?

最佳答案

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

region.center.latitude = [mapLat doubleValue];


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

09-06 11:14