如何将纬度和经度发送给MapViewController?

发布于 2024-11-24 23:15:30 字数 3298 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文