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

來源:互聯網
上載者:User

標籤:des   style   blog   class   code   java   

首先建立一個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、coordinate

  MKAnnotationView:

    在註解裡, 它是用於描述如何顯示註解的, 換句話是確是顯示成藍點還是顯示成圖釘。 在委託方法裡來確定, 如果委託方法,返回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中地圖及定位的基本實現先描述到這裡,後續更新,歡迎瀏覽查詢!

      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.