这两天抽时间学习了一下IOS下谷歌地图的API 现在很多APP中都会使用谷歌的地图。 个人觉得开发起来还是非常的便利的。废话不多说啦,赶快进入今天的正题。如下所示,这个项目我是在iPhone上调试的,这正是我的手机,模拟器上我没有试过,模拟器肯定是能打开谷歌地图的,但是好像不能定位地点。大家仔细看我下面的代码描述,其实很简单 真的很简单。
![IMG_0096](http://www.xuanyusong.com/wp-content/uploads/2012/07/IMG_0096.png)
OK下面是代码片段。
创建一个工程,如下图所示,先将CoreLocation.framework 和 MapKit.framework 引入工程中,前者是负责定位的,后者是负责地图的。
![屏幕快照 2012-07-21 下午10.15.23](http://www.xuanyusong.com/wp-content/uploads/2012/07/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7-2012-07-21-%E4%B8%8B%E5%8D%8810.15.23.png)
AppDelegate.h 入口类,没什么好说的我就不解释了。
1 | #import <UIKit/UIKit.h> |
2 | #import "MapViewController.h" |
4 | @interface AppDelegate : UIResponder <UIApplicationDelegate> |
6 | @property (strong, nonatomic) UIWindow *window; |
7 | @property (strong, nonatomic) UINavigationController *navController; |
8 | @property (strong, nonatomic) UIViewController *viewController; |
AppDelegate.m
01 | #import "AppDelegate.h" |
03 | @implementation AppDelegate |
05 | @synthesize window = _window; |
06 | @synthesize navController; |
07 | @synthesize viewController; |
15 | - ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
17 | self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
19 | self.window.backgroundColor = [UIColor whiteColor]; |
20 | self.viewController = [[MapViewController alloc]init]; |
21 | self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; |
22 | [self.window addSubview:navController.view]; |
24 | [self.window makeKeyAndVisible]; |
主要的东东都写在MapViewController中,请大家仔细看这里。
MapViewController.h
01 | #import <UIKit/UIKit.h> |
02 | #import <MapKit/MKReverseGeocoder.h> |
03 | #import <CoreLocation/CoreLocation.h> |
04 | #import <MapKit/MapKit.h> |
06 | @interface MapViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate> |
10 | MKMapView *myMapView ; |
12 | CLLocationManager *_locManager ; |
MapViewController.m 注意看这个类噢。
001 | #import "MapViewController.h" |
003 | @implementation MapViewController |
007 | [_locManager release]; |
016 | self.navigationItem.title = @ "雨松MOMO" ; |
018 | myMapView = [[[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease]; |
019 | myMapView.delegate = self; |
022 | myMapView.hidden = true ; |
023 | [self.view addSubview:myMapView]; |
026 | _locManager = [[CLLocationManager alloc] init]; |
027 | [_locManager setDelegate:self]; |
028 | [_locManager setDesiredAccuracy:kCLLocationAccuracyBest]; |
032 | -( void ) viewWillAppear:( BOOL )animated |
035 | [super viewWillAppear:animated]; |
044 | [_locManager startUpdatingLocation]; |
049 | - ( void )locationManager:(CLLocationManager *)manager |
050 | didUpdateToLocation:(CLLocation *)newLocation |
051 | fromLocation:(CLLocation *)oldLocation |
056 | CLLocationCoordinate2D loc = [newLocation coordinate]; |
057 | float lat = loc.latitude; |
058 | float lon = loc.longitude; |
061 | myMapView.showsUserLocation =YES; |
064 | [manager stopUpdatingLocation]; |
067 | MKCircle* circle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(myMapView.userLocation.location.coordinate.latitude, myMapView.userLocation.location.coordinate.longitude) radius:5000]; |
072 | [myMapView addAnnotation:circle]; |
075 | CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease]; |
076 | [geocoder reverseGeocodeLocation: _locManager.location completionHandler: |
077 | ^(NSArray *placemarks, NSError *error) { |
080 | CLPlacemark *placemark = [placemarks objectAtIndex:0]; |
082 | NSString *locatedAt = [[placemark.addressDictionary valueForKey:@ "FormattedAddressLines" ] componentsJoinedByString:@ ", " ]; |
086 | [myMapView.userLocation setTitle:locatedAt]; |
087 | [myMapView.userLocation setSubtitle:@ "雨松MOMO在这里噢" ]; |
092 | MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } }; |
093 | theRegion.center= myMapView.userLocation.location.coordinate; |
096 | theRegion.span.longitudeDelta = 0.01f; |
097 | theRegion.span.latitudeDelta = 0.01f; |
099 | [myMapView setRegion:theRegion animated:YES]; |
103 | myMapView.hidden = false ; |
109 | - ( void )locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { |
111 | if ( [error code] == kCLErrorDenied ) |
120 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@ "定位服务已经关闭" |
121 | message:@ "请您在设置页面中打开本软件的定位服务" |
122 | delegate:self cancelButtonTitle:@ "确定" otherButtonTitles:nil, nil]; |
125 | [manager stopUpdatingHeading]; |
127 | else if ([error code] == kCLErrorHeadingFailure) |
134 | - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation; |
136 | static NSString *identifier = @ "com.xys.momo" ; |
138 | MKAnnotationView *pin = [ mapView dequeueReusableAnnotationViewWithIdentifier:identifier ]; |
142 | pin = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:identifier ]; |
145 | pin.image = [ UIImage imageNamed:@ "0.jpg" ]; |
149 | pin.canShowCallout=YES; |
153 | UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; |
154 | pin.rightCalloutAccessoryView=btn; |
157 | pin.annotation = annotation; |
163 | - ( void )viewDidUnload |
165 | [super viewDidUnload]; |
169 | - ( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
171 | return (interfaceOrientation == UIInterfaceOrientationPortrait); |
最后是本文的源码下载:http://vdisk.weibo.com/s/acdN7
雨松MOMO祝大家学习愉快、工作愉快、生活愉快、互相学习与进步,加油~ 话说北京这会应该不下雨了吧??雨停了回家睡觉。 嚯嚯!
———————————-华丽的分割线——————————–
以上方法我在IOS6中使用发现了一点小问题,IOS6使用CLLocationManager定位的时候发现有时候定位到的经纬度是0.0000 所以地图界面中就是一个白屏。那么我将解决的办法贴出来。
2 | - ( void )locationManager:(CLLocationManager *)manager |
3 | didUpdateToLocation:(CLLocation *)newLocation |
4 | fromLocation:(CLLocation *)oldLocation |
7 | myMapView.showsUserLocation =YES; |
用这个方法来接受当前地图经纬度信息
01 | - ( void )mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation |
05 | [_locManager stopUpdatingLocation]; |
08 | MKCircle* circle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(myMapView.userLocation.location.coordinate.latitude, myMapView.userLocation.location.coordinate.longitude) radius:5000]; |
10 | [myMapView addAnnotation:circle]; |
12 | NSLog(@ "%f" ,myMapView.userLocation.location.coordinate.latitude); |
15 | CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease]; |
16 | [geocoder reverseGeocodeLocation: _locManager.location completionHandler: |
17 | ^(NSArray *placemarks, NSError *error) { |
20 | CLPlacemark *placemark = [placemarks objectAtIndex:0]; |
22 | NSString *locatedAt = [[placemark.addressDictionary valueForKey:@ "FormattedAddressLines" ] componentsJoinedByString:@ ", " ]; |
26 | [myMapView.userLocation setTitle:locatedAt]; |
27 | [myMapView.userLocation setSubtitle:@ "你在这里噢" ]; |
29 | nowLatitude = myMapView.userLocation.location.coordinate.latitude; |
30 | nowLongitude = myMapView.userLocation.location.coordinate.longitude; |
35 | MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } }; |
36 | theRegion.center= myMapView.userLocation.location.coordinate; |
42 | theRegion.span.longitudeDelta = 0.01f; |
43 | theRegion.span.latitudeDelta = 0.01f; |
45 | [myMapView setRegion:theRegion animated:YES]; |