iOS地图的使用

来源:互联网 发布:外贸网站cms 编辑:程序博客网 时间:2024/04/29 13:49


一、获取用户的位置


1.首先链接CoreLocation.framework框架,然后在需要定位的地方导入<CoreLocation/CoreLocation.h>头文件

7.0以后要遭plist文件里添加NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

2.在开始使用定位前调用[CLLocationManagerlocationServicesEnabled]判断定位服务能否使用


- (void)getCurrentLocation {

    if(self.locationMarnger ==nil){

       self.locationMarnger = [[CLLocationManageralloc] init];

    }

    //设置地图的精准度

    self.locationMarnger.desiredAccuracy =kCLLocationAccuracyNearestTenMeters;

    //设置超多100米获取新的位置

    self.locationMarnger.distanceFilter =100;

    self.locationMarnger.delegate  =self;

    [self.locationMarngerstartUpdatingLocation];

}


#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 = [[CLGeocoderalloc] init];

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

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

           CLPlacemark *placeMark = placemarks[0];

           //存储反编码的地理信息

           NSDictionary *placeDiction = placeMark.addressDictionary;

           for(NSString *keyin placeDiction.allKeys){

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

            }

        }

    }];

}


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

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

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

   CLGeocoder *geocoder = [[CLGeocoderalloc] init];

   CLCircularRegion *region = [[CLCircularRegionalloc] initWithCenter:coordinateradius:100000identifier:@"region"];


    [geocodergeocodeAddressString: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 = [[MKMapViewalloc] initWithFrame:[UIScreenmainScreen].bounds];

    self.mapView.mapType = MKMapTypeStandard;

   self.mapView.delegate =self;

self.mapView.showsUserLocation =YES;

    [self.viewaddSubview:self.mapView];

    //改变当前地图显示区域

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

    [self.mapViewsetRegion:region animated:YES];


2.添加标注

//向地图添加标注

   MyAnnotation *annottaion = [[MyAnnotationalloc] init];

    annottaion.coordinate = coordinate;

    annottaion.title =@"成功了";

    [self.mapViewaddAnnotation: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 {

    staticNSString *iden =@"annotationView";

   MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapViewdequeueReusableAnnotationViewWithIdentifier:iden];

   if(annotationView == nil){

        annotationView = [[MKPinAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:iden];

        annotationView.canShowCallout =YES;

        annotationView.animatesDrop =YES;

        annotationView.pinColor =MKPinAnnotationColorRed;

    }

   return annotationView;

}






0 0
原创粉丝点击