本文共 6057 字,大约阅读时间需要 20 分钟。
本节书摘来自异步社区《iPad开发从入门到精通》一书中的第6章,第6.5节地图信息,作者 杨春泽,更多章节内容可以访问云栖社区“异步社区”公众号查看
6.5 地图信息
iPad开发从入门到精通通过此功能,可以在地图中查看某条公交线路的信息,这样更具有直观性,大大地方便了用户的浏览。本系统可以分为如下3种显示地图的样式。Standard。Statellite。Hybird。除此之外,还提供了网页地图功能。6.5.1 地图主视图
地图主视图CBus_MapView.xib的UI界面效果如图6-11所示。在上方显示3个选项卡,在下方显示了地图信息。实现文件 CBus_MapViewController.m的主要代码如下所示。
#import "CBus_MapViewController.h"#import "CBus_WebMapViewController.h"#import "CDataContainer.h"@implementation CBus_MapViewController@synthesize cityMapView,mapStyleSegCtr,mapNavigationBar;@synthesize webMapViewController;- (void)dealloc{ [super dealloc]; [cityMapView release]; [mapStyleSegCtr release]; [mapNavigationBar release]; [webMapViewController release];}- (void)didReceiveMemoryWarning{ // 释放视图 [super didReceiveMemoryWarning];}#pragma mark - View lifecycle- (void)viewDidLoad{ [super viewDidLoad]; UIBarButtonItem *webMapButton = [[UIBarButtonItem alloc] initWithTitle:@"WebMap" style:UIBarButtonItemStyleBordered target:self action:@selector(goToWebMap:)]; self.navigationItem.rightBarButtonItem = webMapButton; [webMapButton release]; CLLocationManager *locationMananger = [[CLLocationManager alloc] init]; locationMananger.delegate = self; locationMananger.desiredAccuracy = kCLLocationAccuracyBest; locationMananger.distanceFilter = 1000.0; [locationMananger startUpdatingHeading]; MKCoordinateSpan theSpan; theSpan.latitudeDelta = 0.05; theSpan.longitudeDelta = 0.05; MKCoordinateRegion theRegion; theRegion.center = [[locationMananger location]coordinate]; theRegion.span = theSpan; [cityMapView setRegion:theRegion]; [locationMananger release];}- (IBAction) OnSegmentIndexChanged:(id)sender{ if ([sender selectedSegmentIndex] == 0){ NSLog(@"--------------OnSegmentIndexChanged1-------"); cityMapView.mapType = MKMapTypeStandard; } else if([sender selectedSegmentIndex] == 1){ NSLog(@"--------------OnSegmentIndexChanged2-------"); cityMapView.mapType = MKMapTypeSatellite; } else if([sender selectedSegmentIndex] == 2){ NSLog(@"--------------OnSegmentIndexChanged3-------"); cityMapView.mapType = MKMapTypeHybrid; }}- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; NSInteger styleNum = [userDefault integerForKey:@"styleType"]; switch (styleNum) { case 0:{ [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; self.navigationController.navigationBar.barStyle = UIBarStyleDefault; self.mapStyleSegCtr.tintColor = [UIColor colorWithRed:0.48 green: 0.56 blue:0.66 alpha:1.0]; self.mapNavigationBar.barStyle = UIBarStyleDefault; break; } case 1:{ [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyle BlackOpaque; self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; self.mapStyleSegCtr.tintColor = [UIColor darkGrayColor]; self.mapNavigationBar.barStyle = UIBarStyleBlackOpaque; break; } }}- (IBAction)goToWebMap:(id)sender{ CBus_WebMapViewController *theController = [[CBus_WebMapViewController alloc] initWithNibName:@"CBus_WebMapView" bundle:nil]; self.webMapViewController = theController; [UIView beginAnimations:@"View Flip" context:nil]; [UIView setAnimationDuration:1.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView: self.navigationController.view cache:NO]; [self.navigationController pushViewController:webMapViewController animated:YES]; [UIView commitAnimations]; [theController release];}- (void)viewDidUnload{ [super viewDidUnload]; }- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait);}@end
执行效果如图6-12所示。
6.5.2 Web地图视图
为了便于用户详细浏览公交路线,本项目还提供了Web地图功能,CBus_WebMapView.xibUI界面效果如图6-13所示。实现文件CBus_WebMapViewController.m的主要代码如下所示。
#import "CBus_WebMapViewController.h"@implementation CBus_WebMapViewController@synthesize myWenView;// 视图初始化- (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.hidesBackButton = YES; UIBarButtonItem *mapButton = [[UIBarButtonItem alloc] initWithTitle:@"Map" style:UIBarButtonItemStyleBordered target:selfaction:@selector(goToMap:)]; self.navigationItem.rightBarButtonItem = mapButton; NSMutableString *googleSearch = [NSMutableString stringWithFormat:@"http://ditu. google.cn/maps?f=d&source=s_d&saddr='西安市火车站'&daddr='西安市钟楼'&hl=zh&t=m&dirflg=h"]; [myWenView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[google Search stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]]; [mapButton release];}- (IBAction) goToMap:(id)sender{ [UIView beginAnimations:@"View Flip" context:nil]; [UIView setAnimationDuration:1.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self. navigationController.view cache:NO]; [self.navigationController popViewControllerAnimated:NO]; [UIView commitAnimations];}- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; NSInteger styleNum = [userDefault integerForKey:@"styleType"]; switch (styleNum) { case 0:{ [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; self.navigationController.navigationBar.barStyle = UIBarStyleDefault; break; } case 1:{ [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyle BlackOpaque; self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; break; } }}- (void)didReceiveMemoryWarning { // 释放视图 [super didReceiveMemoryWarning];}- (void)viewDidUnload { [super viewDidUnload]; self.myWenView=nil;}- (void)dealloc { [super dealloc]; [self.myWenView release];}@end
执行效果如图6-14所示。
转载地址:http://yxpvo.baihongyu.com/