地图定位

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



测试效果

        


  


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

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定义

typedef struct{

      CLLocationCoordinate2D center;//表示显示的中心

      MKCoordinateSpan span;       //表示比例

}MKCoordinateRegion;


MKCoordinateSpan定义:


typedef struct{

    CLLocationDegrees latitudeDelta;//这类型在前一节中讲过了,是double型的

    CLLocationDegrees longitudeDlta;

}MKCoordinateSpan;


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

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


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

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

  1. #import <UIKit/UIKit.h>  
  2. #import <CoreLocation/CoreLocation.h>  
  3. #import <MapKit/MapKit.h>  
  4.   
  5. @interface LocationViewController : UIViewController<CLLocationManagerDelegate>  
  6. @property (strong,nonatomic) CLLocationManager *locationManager;  
  7. @property (strong,nonatomic) CLLocation *startPoint;  
  8.   
  9. @property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;  
  10. @property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;  
  11. @property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;  
  12. @property (strong, nonatomic) IBOutlet UILabel *altitudeLabel;  
  13.   
  14. @property (strong, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;  
  15. @property (strong, nonatomic) IBOutlet UILabel *distanceTraveledLabel;  
  16.   
  17. @property (strong, nonatomic) MKMapView *mapView;  
  18. @property (strong, nonatomic) UISegmentedControl *mapSegmentedControl;  
  19. - (IBAction)mapViewSwitcher:(id)sender;  
  20. -(void) selectedMapType:(id)sender;  
  21.   
  22. @end  


  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     locationManager = [[CLLocationManager alloc] init];  
  6.     locationManager.delegate = self;  
  7. //    设置位置精度  
  8.     locationManager.desiredAccuracy = kCLLocationAccuracyBest;  
  9. //    距离过滤,表示在地图上每隔100更新一次定位  
  10.     locationManager.distanceFilter = 100;  
  11.     //    启动位置管理器,进行定位服务  
  12.     [locationManager startUpdatingLocation];  
  13.   
  14.       
  15.       
  16. //    设置mapSegmentedControl按钮样式和显示按钮显示文字  
  17.     NSArray *mapItems = [[NSArray alloc] initWithObjects:@"街道地图",@"卫星地图",@"混合地图", nil];  
  18.     mapSegmentedControl = [[UISegmentedControl alloc] initWithItems:mapItems];  
  19.     mapSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;  
  20.     mapSegmentedControl.frame = CGRectMake(0, 430, 180, 30);  
  21. //    根据索引值,表示mapSegmentedControl默认响应哪个  
  22.     mapSegmentedControl.selectedSegmentIndex = 0;  
  23. //    给mapSegmentedContol添加响应方法  
  24.     [mapSegmentedControl addTarget:self action:@selector(selectedMapType:) forControlEvents:UIControlEventValueChanged];  
  25.       
  26. }  

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

  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.      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Python绘制混淆矩阵,可以使用sklearn.metrics包中的confusion_matrix函数。首先,需要将预测结果和真实标签以类似的格式赋值给y_pred和y_true变量。然后,可以使用confusion_matrix函数生成混淆矩阵C,可以通过labels参数指定类别的标签。接下来,可以使用matplotlib.pyplot中的函数绘制矩阵图,使用plt.matshow(C, cmap=plt.cm.Reds)来展示混淆矩阵的颜色。可以使用plt.annotate函数在矩阵图中显示每个元素的值。最后,可以使用plt.xlabel和plt.ylabel函数设置x轴和y轴的标签。最后,使用plt.show函数显示绘制好的混淆矩阵图。 [1 [2] 示例代码如下: ```python from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt y_pred = [] # 预测结果 y_true = [] # 真实标签 C = confusion_matrix(y_true, y_pred, labels=['0','1','2','3','4']) plt.matshow(C, cmap=plt.cm.Reds) for i in range(len(C)): for j in range(len(C)): plt.annotate(C[j, i], xy=(i, j), horizontalalignment='center', verticalalignment='center') plt.ylabel('True label') plt.xlabel('Predicted label') plt.show() ``` 这段代码会根据给定的预测结果和真实标签生成混淆矩阵,并使用矩阵图展示混淆矩阵的颜色。每个元素表示预测为某个类别的样本数量。通过调整代码中的参数和标签,可以根据不同的需求进行自定义。 [1 [2 [3<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [详解使用python绘制混淆矩阵confusion_matrix)](https://download.csdn.net/download/weixin_38580959/12861679)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [利用python绘制混淆矩阵](https://blog.csdn.net/weixin_43818631/article/details/121309660)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值