How can I pass latitude and longitude values from UIViewController to MKMapView?
- by jerincbus
I have a detail view that includes three UIButtons, each of which pushes a different view on to the stack. One of the buttons is connected to a MKMapView. When that button is pushed I need to send the latitude and longitude variables from the detail view to the map view. I'm trying to add the string declaration in the 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 on 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 on the pertinent 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 both lat and lng variables. So my questions are am I going about passing the variables from one view to another the right way? And does the MKMapView accept latitude and longitude as a string or a number?