gpt4 book ai didi

iphone - 如何将纬度和经度值从 UIViewController 传递到 MKMapView?

转载 作者:行者123 更新时间:2023-12-03 20:29:31 27 4
gpt4 key购买 nike

我有一个包含三个 UIButton 的详细 View ,每个按钮都将不同的 View 推送到堆栈上。其中一个按钮连接到 MKMapView。当按下该按钮时,我需要将纬度和经度变量从详细 View 发送到 map View 。我正在尝试在 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;

}

在我的 MapViewController.h 文件中,我有:

#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

关于 MapViewController.m 文件的相关部分,我有:

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

}

但是当我尝试为 lat 和 lng 变量构建“错误:赋值中的类型不兼容”时,我得到了这个信息。所以我的问题是我是否要以正确的方式将变量从一个 View 传递到另一个 View ? MKMapView 接受纬度和经度作为字符串还是数字?

最佳答案

MapKit 中的纬度和经度存储为 CLLocationDegrees 类型,定义为 double。要将 NSNumber 转换为 double ,请使用:

region.center.latitude = [mapLat doubleValue];

或者,也许更好,从一开始就将您的属性声明为 CLLocationDegrees

关于iphone - 如何将纬度和经度值从 UIViewController 传递到 MKMapView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2669571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com