如何将纬度和经度发送给MapViewController?
我有 DetailView,它从 plist
文件读取并显示视图上的所有值,我的代码如下,但是,我想将纬度和经度值传递给 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
I have the DetailView which read from the plist
file and show all the value on the View, my code as below, however, I want to pass the latitude and longitude value to MapViewController
was not success, please tell me which was miss or wrong?
DetailViewController.h
@interface DetailViewController : UIViewController {
.............
.............
NSNumber *mapLat;
NSNumber *mapLon;
}
@property (nonatomic,retain) NSNumber *mapLat;
@property (nonatomic,retain) NSNumber *mapLon;
@end
For the 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;
}
For the MapviewController.h code as below:
#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
For the MapviewController.m code as below:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论