2

我有一個詳細視圖,其中包含三個UIButtons,每個UIButtons將不同視圖推入堆棧。其中一個按鈕連接到MKMapView。當按下該按鈕時,我需要將詳細視圖中的緯度和經度變量發送到地圖視圖。我想補充的IBAction爲字符串聲明:如何將UIViewController的緯度和經度值傳遞給MKMapView?

- (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:assa中的不兼容類型nment'表示lat和lng變量。所以我的問題是關於如何將正確的方法從一個視圖傳遞到另一個視圖?而且MKMapView是以字符串還是數字的形式接受經緯度?

回答

6

MapKit中的緯度和經度存儲爲CLLocationDegrees類型,其定義爲double。爲了您的NSNumbers轉換爲雙打,使用方法:

region.center.latitude = [mapLat doubleValue]; 

,或者更好,因爲CLLocationDegrees從一開始就宣佈你的屬性。

相關問題