如何将经度和经度发送到MapViewController?

时间:2011-07-17 14:53:08

标签: mkmapviewdelegate

我有一个DetailView,它从plist文件中读取并显示View上的所有值,我的代码如下,但是,我想将纬度和经度值传递给MapViewController不是成功,请告诉我哪些是错过或错了?

DetailViewController.h

@interface DetailViewController : UIViewController {

.............
.............
NSNumber *mapLat;
NSNumber *mapLon;
}

@property (nonatomic,retain)  NSNumber *mapLat;
@property (nonatomic,retain)  NSNumber *mapLon;

@end

对于DetailViewController.m

#import "DetailViewController.h"
#import "MapViewController.h"

@implementation DetailViewController

@synthesize mapLat,mapLon;DetailViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    .........

NSNumber *lat = [[NSString alloc] initWithFormat:@"%@", appDelegate.latitudeText];
NSLog (@"%@", lat); //i can read the latitude value from plist without any problem

NSNumber *lon = [[NSString alloc] initWithFormat:@"%@", appDelegate.longitudeText];
NSLog (@"%@", lon); //i can read the longitude value from plist without any problem

UIBarButtonItem *mapButton = [[UIBarButtonItem alloc] initWithTitle:@"Map"    style:UIBarButtonItemStyleBordered target:self action:@selector(btnViewMap)];
 self.navigationItem.rightBarButtonItem = mapButton;


}

对于MapviewController.h代码如下:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "AddressAnnotation.h"
#import "DetailViewController.h"

@interface MapViewController: UIViewController {

IBOutlet MKMapView *mapView;
AddressAnnotation *addAnnotation;

NSNumber *mapLat;
NSNumber *mapLon;
}

@property (nonatomic,retain) NSNumber *mapLat;
@property (nonatomic,retain) NSNumber *mapLon;

- (IBAction) showAddress;

@end

对于MapviewController.m代码如下:

#import "MapViewController.h"
#import "AddressAnnotation.h"
#import "DetailViewController.h"

@implementation MapViewController

@synthesize mapLat,mapLon;

- (void)viewDidLoad {
[super viewDidLoad];

NSLog (@"%@", mapLat); //why the value was 0 pass from DetailViewController
NSLog (@"%@", mapLon); //why the value was 0 pass from DetailViewController
[self showAddress];
}


- (IBAction) showAddress {
 MKCoordinateRegion region;
 MKCoordinateSpan span;
 span.latitudeDelta =0.2;
 span.longitudeDelta =0.2;

 CLLocationCoordinate2D location = mapView.userLocation.coordinate;


 location.latitude = [mapLat doubleValue];
 NSLog (@"%f", location.latitude);
 location.longitude = [mapLon doubleValue]; 

 region.span = span;
 region.center = location;

 if (addAnnotation !=nil) {
 [mapView removeAnnotation:addAnnotation];
 [addAnnotation release];
 addAnnotation = nil;
 }

 addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
 [mapView addAnnotation:addAnnotation];

 [mapView setRegion:region animated:TRUE];
 [mapView regionThatFits:region];

 }


- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation: (id    <MKAnnotation>) annotation {

 MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
 annView.pinColor = MKPinAnnotationColorGreen;
 annView.animatesDrop = TRUE;
 annView.canShowCallout = YES;
 annView.calloutOffset = CGPointMake (-5,5);
 return annView;
}


- (void)dealloc {
  [super dealloc];
}


@end

0 个答案:

没有答案