苹果地图显示默认区域
Posted
技术标签:
【中文标题】苹果地图显示默认区域【英文标题】:Apple maps show default region 【发布时间】:2013-04-03 20:40:33 【问题描述】:我想为应用程序设置一个默认区域,每次打开地图时它都应该显示一个区域。例如,每次我打开地图时,它都应该显示“伦敦”。
我这样做了,但它没有居中
@interface AppleMapViewController ()
@end
@implementation TicinoWineAppleMapViewController
@synthesize mapView = _mapView;
#define DEFAULT_LATITUDE 46.006512
#define DEFAULT_LONGITUDE 8.952312
#define THE_SPAN 10.0f
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
return self;
- (void)viewWillAppear:(BOOL)animated
NSLog(@"ViewWillAppear");
- (void)createDefaultRegion
MKCoordinateRegion defaultRegion;
CLLocationCoordinate2D center;
center.latitude = DEFAULT_LATITUDE;
center.longitude = DEFAULT_LONGITUDE;
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
// defaultRegion.center = center;
// defaultRegion.span = span;
defaultRegion = MKCoordinateRegionMake(center, span);
TicinoWineMapViewAnnotation *annotation = [[TicinoWineMapViewAnnotation alloc]initWithTitle:@"ciao" andCoordinate:center];
[_mapView addAnnotation:annotation];
[_mapView setRegion:defaultRegion animated:YES];
[_mapView setCenterCoordinate:center animated:YES];
- (void)viewDidLoad
[super viewDidLoad];
[self createDefaultRegion];
@end
两种方法我都试过了:
[_mapView setRegion:defaultRegion animated:YES];
[_mapView setCenterCoordinate:center animated:YES];
但它并没有以我想要的为中心。这可能是模拟器的问题?
【问题讨论】:
你在真机上试过了吗? 不,我还在用模拟器 【参考方案1】:你可以试试这个功能。它对我有用。
(void)recenterMap
NSArray *coordinates = [Mapsview valueForKeyPath:@"annotations.coordinate"];
CLLocationCoordinate2D maxCoord = -90.0f, -180.0f; CLLocationCoordinate2D minCoord = 90.0f, 180.0f;
for(NSValue *坐标值)
CLLocationCoordinate2D coord = 0.0f, 0.0f;
[value getValue:&coord];
if(coord.longitude > maxCoord.longitude)
maxCoord.longitude = coord.longitude;
if(coord.latitude > maxCoord.latitude)
maxCoord.latitude = coord.latitude;
if(coord.longitude < minCoord.longitude)
minCoord.longitude = coord.longitude;
if(coord.latitude < minCoord.latitude)
minCoord.latitude = coord.latitude;
MKCoordinateRegion 区域 = 0.0f, 0.0f, 0.0f, 0.0f;
//region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0; // region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
region.center.longitude = minCoord.longitude + (maxCoord.longitude - minCoord.longitude)/2.0f; region.center.latitude = minCoord.latitude + (maxCoord.latitude - minCoord.latitude)/2.0f;
region.span.longitudeDelta = (maxCoord.longitude - minCoord.longitude) * 1.5; region.span.latitudeDelta = (maxCoord.latitude - minCoord.latitude) * 1.5;
[Mapsview setRegion:region animated:YES];
【讨论】:
在设备上试用效果最佳。以上是关于苹果地图显示默认区域的主要内容,如果未能解决你的问题,请参考以下文章