国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS地圖及定位功能基本實現的詳盡描述

2019-11-14 20:29:37
字體:
來源:轉載
供稿:網友

首先創建一個viewController:

添加MapKit,CoreLocation框架

一、添加地圖

1、初始化地圖視圖:

@PRoperty (nonatomic, strong) MKMapView *mapView;  self.mapView =[[MKMapView alloc]initWithFrame:self.view.bounds];

 

2、設置地圖的顯示類型,三種:標準,衛星地圖,混合  

self.mapView.mapType  {        MKMapTypeStandard = 0,        MKMapTypeSatellite,        MKMapTypeHybrid };

 

3、定義地圖是否可以放大縮小滑動,添加代理    

self.mapView.zoomEnabled = YES;self.mapView.scrollEnabled = YES;

self.mapView.delegate = self;

 

4、添加CoreLocation框架

  添加一個地區的地理坐標位置(經緯度),北緯是正數,東經是正數;當前坐標表示鄭州市位置   

CLLocationCoordinate2D coordinate;coordinate.latitude = 34.7598711;coordinate.longitude = 113.663221;

5、表示顯示區域的精度,值越小表示的范圍越精確,值越大表示的范圍越大,但是不精確

MKCoordinateSpan span = {100,100};    100在地圖上表示100*111公里的范圍

6、根據地理坐標和span創建表示區域的值。(通常一個圓,和半徑) 

初始化方法(1):MKCoordinateRegion region = {coordinate,span};     (2):MKCoordinateRegion region;                  region =MKCoordinateRegionMake(coordinate,span);

 

7、告訴地圖顯示的區域,并講地圖添加到當前視圖上

[self.mapView setRegion:region];[self.view addSubview:self.mapView];

8、為坐標位置添加注解

MKPointAnnotation *pinAnnotation = [[MKPointAnnotation alloc] init];pinAnnotation.coordinate = coordinate; pinAnnotation.title = @"鄭州"; pinAnnotation.subtitle = @"河南青云信息技術"; [self.mapView addAnnotation:pinAnnotation];

 

9、當要對顯示當注解自定義時,調用下面方法,用來設置顯示界面

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{    if ([annotation isKindOfClass:[MKUserLocation class]]) {        return nil;    }    static NSString *indentifier = @"pinView";    MKPinAnnotationView *pinView =(MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:indentifier];    if (nil == pinView) {        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:indentifier];    }    pinView.pinColor = MKPinAnnotationColorPurple;    pinView.animatesDrop = YES;    pinView.canShowCallout = YES;        return pinView;}

 

10、MKAnnotation 與 MKAnnotationView 的差異  

MKAnnotation:    它在注解里實際上就是一個model,主要用于向視圖提供相應的數據。 默認情況下,有三個屬性:1?title 2.subtitle 3、coordinateMKAnnotationView:    在注解里, 它是用于描述如何顯示注解的, 換句話是確定是顯示成藍點還是顯示成大頭針。 在委托方法里來確定, 如果委托方法,返回nil,這個時候藍點。如果想要顯示成其它的視圖,必須在代碼里來實現

 

 

二、添加位置管理

1、初始化,并添加代理,設置精度

@property (nonatomic, strong) CLLocationManager *locationManager;       self.locationManager = [[CLLocationManager alloc] init];     self.locationManager.delegate = self;     self.locationManager.distanceFilter = 500; //對于位置管理來說,并非是精度越高越好。因為GPS模塊是手機最耗電的模塊,所以為最大限度讓我們應用程序省電,需要根據具體的應用來設定具體的精度
   self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;        [self.locationManager startUpdatingLocation];

2、當位置更新時調用下面方法,可得出新舊兩位置間的距離,新位置的坐標;并將地圖顯示在新坐標位置

- (void)locationManager:(CLLocationManager *)manager    didUpdateToLocation:(CLLocation *)newLocation           fromLocation:(CLLocation *)oldLocation{    NSLog(@"%@",NSStringFromSelector(_cmd));     CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];    NSLog(@"distance is %f",distance /1000.0);        MKCoordinateRegion region = MKCoordinateRegionMake(newLocation.coordinate, MKCoordinateSpanMake(100, 100));    [self.mapView setRegion:region animated:YES];         }

3、如果位置管理器失效,將調用下面方法并返回錯誤信息

- (void)locationManager:(CLLocationManager *)manager       didFailWithError:(NSError *)error{    NSLog(@"%@",error);}

 

小結:關于iOS中地圖及定位的基本實現先描述到這里,后續更新,歡迎瀏覽查詢!

      


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 朝阳县| 延津县| 尼木县| 常宁市| 调兵山市| 商河县| 剑河县| 綦江县| 定远县| 民权县| 齐河县| 红桥区| 鸡东县| 来安县| 泽库县| 柳州市| 吴旗县| 桐柏县| 宜兰市| 布拖县| 海南省| 攀枝花市| 潮安县| 德庆县| 嵊州市| 三穗县| 吉水县| 英超| 永新县| 梅河口市| 阿城市| 天门市| 广河县| 湄潭县| 黎城县| 丹巴县| 滁州市| 棋牌| 和平区| 苏尼特右旗| 昭苏县|