iOS地圖的使用


一、獲取用戶的位置


1.首先鏈接CoreLocation.framework框架,然後在需要定位的地方導入<CoreLocation/CoreLocation.h>頭文件

7.0以後要遭plist文件裏添加 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

2.在開始使用定位前調用[CLLocationManager locationServicesEnabled]判斷定位服務能否使用


- (void)getCurrentLocation {

    if(self.locationMarnger == nil){

        self.locationMarnger = [[CLLocationManager alloc] init];

    }

    //設置地圖的精準度

    self.locationMarnger.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

    //設置超多100米獲取新的位置

    self.locationMarnger.distanceFilter = 100;

    self.locationMarnger.delegate  = self;

    [self.locationMarnger startUpdatingLocation];

}


#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray *)locations {

    NSLog(@"*********success*********");

    //停止定位

    [manager stopUpdatingLocation];

    //獲取設備當前位置

    CLLocation *location = [locations lastObject];

    CLLocationCoordinate2D coordinate = location.coordinate;

    NSLog(@"coordinate:  latitude = %f,longitude = %f",coordinate.latitude,coordinate.longitude);

}


- (void)locationManager:(CLLocationManager *)manager

       didFailWithError:(NSError *)error{

    NSLog(@"*********fail*********");

}


注:@property (nonatomic,strong) CLLocationManager *locationMarnger;爲全局變量




二、反編碼

引入AddressBook.framework框架,並在頭文件導入<AddressBook/AddressBook.h>

在定位成功的時候調用下面的方法

#pragma mark - 進行反編碼

- (void)reverseLocation:(CLLocation *)location {

    

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {

        if(placemarks.count > 0 && error == nil){

            CLPlacemark *placeMark = placemarks[0];

            //存儲反編碼的地理信息

            NSDictionary *placeDiction = placeMark.addressDictionary;

            for(NSString *key in placeDiction.allKeys){

                NSLog(@"%@ = %@",key,placeDiction[key]);

            }

        }

    }];

}


三、通過地址得到地理信息

#pragma mark - 通過地址的到地理信息

- (void)getAddressInformation:(NSString *)string currenCoordinate:(CLLocationCoordinate2D)coordinate {

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:100000 identifier:@"region"];


    [geocoder geocodeAddressString:string inRegion:region completionHandler:^(NSArray *placemarks, NSError *error) {

        if(placemarks.count > 0 && error == nil){

            CLPlacemark *placeMark = placemarks[0];

            //存儲反編碼的地理信息

            NSDictionary *placeDiction = placeMark.addressDictionary;

            NSLog(@"address name = %@",placeDiction[@"Name"]);

            CLLocationCoordinate2D coordinate = placeMark.location.coordinate;

            NSLog(@"coordinate:  latitude = %f,longitude = %f",coordinate.latitude,coordinate.longitude); 

        }

    }];

}


四、使用地圖

添加MapKit.framework框架,在使用的圖的地方導入<MapKit/MapKit.h>


1.添加地圖

self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.mapView.mapType = MKMapTypeStandard;

    self.mapView.delegate = self;

self.mapView.showsUserLocation = YES;

    [self.view addSubview:self.mapView];

    //改變當前地圖顯示區域

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinate, 10000, 10000);

    [self.mapView setRegion:region animated:YES];


2.添加標註

//向地圖添加標註

    MyAnnotation *annottaion = [[MyAnnotation alloc] init];

    annottaion.coordinate = coordinate;

    annottaion.title = @"成功了";

    [self.mapView addAnnotation:annottaion];


MyAnnotation的類定義,必須實現MKAnnotation協議

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>


@interface MyAnnotation : NSObject<MKAnnotation>


@property (nonatomic,assign) CLLocationCoordinate2D coordinate;

@property (nonatomic,copy) NSString *title;


@end



3.實現mapView的代理方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString *iden = @"annotationView";

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:iden];

    if(annotationView == nil){

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:iden];

        annotationView.canShowCallout = YES;

        annotationView.animatesDrop = YES;

        annotationView.pinColor = MKPinAnnotationColorRed;

    }

    return annotationView;

}






發佈了34 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章