9Rgoogle地图定位小Demo

好几天没写博客了,今天写的是一个简单功能的google地图小demo,因为模拟器定位的经纬度默认设置为苹果公司地址,在调试--》位置  里可以修改地图的其他位置定位

1365848565_3509.png



测试效果

1365848832_4842.png

  

1365848839_4875.png

      


1365848844_7566.png

  

1365848849_8401.png




实现地图定位我们需要两个类

CLLocationManager  和CLLocation


1.首先需要一个位置管理器,CLLocationManager对象locationManager,CLLocationManager *locationManager;

设置委托   locationManager.delegate =self;


2.设置精度   locationManager.desiredAccuracy =kCLLocationAccuracyBest;

desiredAccuracy类型double,因此我们可以指定他的精度为一个确却数据比如10,但是事件不能计算那么准确,或者由于其他原因都达不到我们所设置进度,因此我们指定为kCLLocationAccuracyBest表示级别最高精度,还有其他精度


kCLLocationAccuracyBestForNavigation  最高精度,这种级别用于导航程序

kCLLocationAccuracyBest  最高精度

kCLLocationAccuracyHundredMeters 精度为100米内

kCLLocationAccuracyKilometer   精度到公里范围内

kCLLocationAccuracyNearestTenMeters   精度到10米内

kCLLocationAccuracyThreeKilometers  精度到3公里范围内


3.设置距离筛选器   locationManager.distanceFilter =100;

距离筛选器,作用是当你移动一段位移后,所以移动距离大于筛选器说设置100m时候,通知委托更新位置;

但是位置一段更新过于频繁就会消耗电池电量,我们可以返回它的默认的没有筛选器模式,使用常量kCLDistanceFilterNone;

locationManager.distanceFilterkCLDistanceFilterNone


4.然后就是启动 位置管理器进行定位[locationManagerstartUpdatingLocation];  如果我们不需要继续轮询更新位置可以使用[locationManager stopUpdatingLocation];停止更新,否则应用程序打开会一直更新,这些都需要添加委托的,遵循


CLLocationManagerDelegate协议


5.为了显示确实却是进行了定位,我们获取定位的经纬度,显示到label上,然后就是用到了CLLocation类,他有五个属性

latitude经度    longitude纬度    horizontalAccuracy水平精度(map中显示的蓝色圆半径都是以它为半径的)  altitude海拔高度

verticalAccuracy  竖直高度(为负值得时候表示无法确定高度)


6.显示定位的实况地图

需要设置显示区域和显示比例,他们是两个结构体,MKCoordinateSpan  ,MKCoordinateRegion

MKCoordinateRegion定义

  1. typedef struct{
  2.       CLLocationCoordinate2D center;//表示显示的中心
  3.       MKCoordinateSpan span;       //表示比例
  4. }MKCoordinateRegion;
复制代码

MKCoordinateSpan定义:

  1. typedef struct{
  2.     CLLocationDegrees latitudeDelta;//这类型在前一节中讲过了,是double型的
  3.     CLLocationDegrees longitudeDlta;
  4. }MKCoordinateSpan;
复制代码

7.通过 region.center = newLocation.coordinate;获取定位的经纬度,然后显示到MKMapView,通过

UISegmentedControl设计了一个按钮集合来切换三种不同地图


首先我们要在工程添加支持定位和显示地图的库

CoreLocation.framework 和 MapKit.framework 添加两个头文件,对应代码

  1. #import 
  2. #import 
  3. #import 

  4. @interface LocationViewController : UIViewController
  5. @property (strong,nonatomic) CLLocationManager *locationManager;
  6. @property (strong,nonatomic) CLLocation *startPoint;

  7. @property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
  8. @property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
  9. @property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;
  10. @property (strong, nonatomic) IBOutlet UILabel *altitudeLabel;

  11. @property (strong, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;
  12. @property (strong, nonatomic) IBOutlet UILabel *distanceTraveledLabel;

  13. @property (strong, nonatomic) MKMapView *mapView;
  14. @property (strong, nonatomic) UISegmentedControl *mapSegmentedControl;
  15. - (IBAction)mapViewSwitcher:(id)sender;
  16. -(void) selectedMapType:(id)sender;

  17. @end
复制代码
  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];

  4.     locationManager = [[CLLocationManager alloc] init];
  5.     locationManager.delegate = self;
  6. //    设置位置精度
  7.     locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  8. //    距离过滤,表示在地图上每隔100更新一次定位
  9.     locationManager.distanceFilter = 100;
  10.     //    启动位置管理器,进行定位服务
  11.     [locationManager startUpdatingLocation];

  12.     
  13.     
  14. //    设置mapSegmentedControl按钮样式和显示按钮显示文字
  15.     NSArray *mapItems = [[NSArray alloc] initWithObjects:@"街道地图",@"卫星地图",@"混合地图", nil];
  16.     mapSegmentedControl = [[UISegmentedControl alloc] initWithItems:mapItems];
  17.     mapSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
  18.     mapSegmentedControl.frame = CGRectMake(0, 430, 180, 30);
  19. //    根据索引值,表示mapSegmentedControl默认响应哪个
  20.     mapSegmentedControl.selectedSegmentIndex = 0;
  21. //    给mapSegmentedContol添加响应方法
  22.     [mapSegmentedControl addTarget:self action:@selector(selectedMapType:) forControlEvents:UIControlEventValueChanged];
  23.     
  24. }
复制代码
  1. #pragma mark -
  2. #pragma mark CLLocationManagerDelegate Methods

  3. //获取位置信息
  4. -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
  5. {
  6.     if (startPoint == nil) {
  7.         startPoint=newLocation;
  8.     }
  9. //    %g表示浮点数 \u00B0表示是转换字符,度的表示
  10. //    显示经度,将经度信息显示到Label上
  11.     latitudeLabel.text = [NSString stringWithFormat:@"%g\u00B0",newLocation.coordinate.latitude];
  12. //显示纬度
  13.     longitudeLabel.text = [NSString stringWithFormat:@"%g\u00B0",newLocation.coordinate.longitude];
  14. //    水平经度,也就是显示经纬度的经度
  15.     horizontalAccuracyLabel.text = [NSString stringWithFormat:@"%gm",newLocation.horizontalAccuracy];
  16. //表示海拔
  17.     altitudeLabel.text = [NSString stringWithFormat:@"%gm",newLocation.altitude];
  18. //  垂直经度
  19.     verticalAccuracyLabel.text = [NSString stringWithFormat:@"%gm",newLocation.verticalAccuracy];
  20. //    CLLocation属性CLLocationDistance的对象计算两个位置间的距离
  21.     CLLocationDistance distance = [newLocation distanceFromLocation:startPoint];
  22. //    将distance显示到Label上
  23.     distanceTraveledLabel.text = [NSString stringWithFormat:@"%gm",distance];
  24.     
  25. //    显示比例,MKCoordinateSpan类型的结构。 它有两个程序叫做 latitudeDelta and longitudeDelta。 这两个程序被用来设定地图的缩放级别——在center周围应该显示多大的区域。
  26.     MKCoordinateSpan span;
  27.     span.latitudeDelta=0.05;
  28.     span.longitudeDelta=0.05;
  29.     
  30.     MKCoordinateRegion region;
  31. //    获取定位的经纬度
  32.     region.center = newLocation.coordinate;
  33. //    显示区域
  34.     region.span = span;
  35.     
  36.     [mapView setRegion:region animated:YES];


  37. }
复制代码
  1. //当设备无法定位当前我位置时候调用此方法
  2. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
  3. {
  4.     NSString *errorType = (error.code == kCLErrorDenied)?@"Access Denied" : @"Unknown Error";
  5.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting Location"
  6.                                                     message:errorType
  7.                                                    delegate:nil
  8.                                           cancelButtonTitle:@"oKay"
  9.                                           otherButtonTitles: nil];
  10.     [alert show];
  11. }
复制代码
通过按钮跳转视图,显示地图
  1. - (IBAction)mapViewSwitcher:(id)sender {
  2.     
  3.     [UIView beginAnimations:@"Curl" context:nil];
  4.     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  5.     [UIView setAnimationDuration:1.25];
  6.     [UIView setAnimationTransition:UIViewAnimationOptionTransitionCurlUp forView:self.view cache:YES];
  7.     mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  8.     //    显示用户位置
  9.     mapView.showsUserLocation = YES;
  10.     
  11.     
  12. //      mapView.mapType = MKMapTypeSatellite;
  13.     /*
  14.      设置地图类型
  15.      mapView.mapType = MKMapTypeStandard;  标准街道地图
  16.      mapView.mapType = MKMapTypeSatellite;  卫星地图
  17.      mapView.mapType = MKMapTypeHybrid;    混合地图
  18.      
  19.      */    
  20.     [self.view addSubview:mapView];
  21.     [mapView addSubview:mapSegmentedControl];
  22.     
  23.     [UIView commitAnimations];
  24. }
复制代码
切换google三种地图显示
  1. -(void)selectedMapType:(id)sender
  2. {   
  3.     UISegmentedControl *control = (UISegmentedControl *)sender;
  4.     if (mapSegmentedControl) {
  5.         
  6.     
  7.     if (control.selectedSegmentIndex == 0) {
  8.         [UIView beginAnimations:@"Curl" context:nil];
  9.         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  10.         [UIView setAnimationDuration:1.25];
  11.         [UIView setAnimationTransition:UIViewAnimationOptionTransitionCurlUp forView:self.view cache:YES];
  12.         mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  13.         //    显示用户位置
  14.         mapView.showsUserLocation = YES;
  15.         mapView.mapType = MKMapTypeStandard;
  16.         
  17.         [self.view addSubview:mapView];
  18.         [mapView addSubview:mapSegmentedControl];
  19.         
  20.         [UIView commitAnimations];
  21.         NSLog(@"----0-->%d",mapSegmentedControl.selectedSegmentIndex);
  22.     }
  23.     else if (control.selectedSegmentIndex==1) {
  24.         [UIView beginAnimations:@"Curl" context:nil];
  25.         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  26.         [UIView setAnimationDuration:1.25];
  27.         [UIView setAnimationTransition:UIViewAnimationOptionTransitionCurlUp forView:self.view cache:YES];
  28.         mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  29.         //    显示用户位置
  30.         mapView.showsUserLocation = YES;
  31.         mapView.mapType = MKMapTypeSatellite;
  32.         
  33.         //    mapView.mapType = MKMapTypeSatellite;
  34.           
  35.         [self.view addSubview:mapView];
  36.         [mapView addSubview:mapSegmentedControl];
  37.         
  38.         [UIView commitAnimations];
  39.         NSLog(@"----1-->%d",mapSegmentedControl.selectedSegmentIndex);
  40.     }
  41.     else if (control.selectedSegmentIndex == 2) {
  42.         [UIView beginAnimations:@"Curl" context:nil];
  43.         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  44.         [UIView setAnimationDuration:1.25];
  45.         [UIView setAnimationTransition:UIViewAnimationOptionTransitionCurlUp forView:self.view cache:YES];
  46.         mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
  47.         //    显示用户位置
  48.         mapView.showsUserLocation = YES;
  49.         mapView.mapType = MKMapTypeHybrid;
  50.         
  51.         //    mapView.mapType = MKMapTypeSatellite;
  52.          
  53.         [self.view addSubview:mapView];
  54.         [mapView addSubview:mapSegmentedControl];
  55.         
  56.         [UIView commitAnimations];
  57.         NSLog(@"----2-->%d",mapSegmentedControl.selectedSegmentIndex);
  58.     }
  59.         }
  60.    
  61. }
复制代码

源代码:http://download.csdn.net/detail/duxinfeng2010/4513843


转自 http://blog.csdn.net/duxinfeng2010/article/details/7887871
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值