How can I pass latitude and longitude values ​​from UIViewController to MKMapView?

I have a detail view that includes three UIButtons, each one piling a different view onto the stack. One of the buttons is connected to MKMapView. When this button is clicked, I need to send the latitude and longitude variables from the detail view to the map view. I am trying to add a line declaration to an 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;

      

}

And in my MapViewController.h file I have:

#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

      

And in the relevant parts of the MapViewController.m file I have:

@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];

}

      

But I get this when I try to build: "error: incompatible types in assignment" for lat and lng variables. So my questions are: Am I going to translate variables from one look to another correctly? And does MKMapView accept latitude and longitude as string or number?

+2


a source to share


1 answer


Latitude and longitude in MapKit are stored as CLLocationDegrees

, which are defined as double

. To convert NSNumbers to paired use:

region.center.latitude = [mapLat doubleValue];

      



Or perhaps it is better to declare your properties as CLLocationDegrees

from the beginning.

+6


a source







All Articles