苹果的定位服务

 

直接上代码

 

头文件

#import <UIKit/UIKit.h>

#import <CoreLocation/CoreLocation.h>

#import <AddressBook/AddressBook.h>

 

@interface ViewController : UIViewController<CLLocationManagerDelegate>

{

    CLLocationManager *locationManger;

}

 

@end

 

.m文件

 

//

//  ViewController.m

//  Location

//

//  Created by zgw on 13-10-31.

//  Copyright (c) 2013 zhao. All rights reserved.

//

 

#import "ViewController.h"

 

@interfaceViewController ()

 

@end

 

@implementation ViewController

 

#pragma mark - 系统函数

- (void)viewDidLoad

{

    [superviewDidLoad];

self.view.backgroundColor = [UIColorwhiteColor];

    locationManger = [[CLLocationManageralloc]init];

    locationManger.delegate = self;

    locationManger.desiredAccuracy = kCLLocationAccuracyBest;

    locationManger.distanceFilter = 1.f;

    [locationMangerstartUpdatingLocation];

}

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark CLLocationMangerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{  

//locations是定位集合

    CLLocation *location = [locations lastObject];

//进行反编码 从经纬度到省市街道

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

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

        if(placemarks.count <= 0) return ;

        CLPlacemark *placemark = [placemarks objectAtIndex:0];

        NSDictionary *addressdictionary = placemark.addressDictionary;

        NSString *address =[addressdictionary objectForKey:(NSString*)kABPersonAddressStreetKey];

        address = address==nil?@"":address;

        

        NSString *state = [addressdictionary objectForKey:(NSString*)kABPersonAddressStateKey];

        state = state == nil?@"":state;

        

        NSString *city = [addressdictionary objectForKey:(NSString*)kABPersonAddressCityKey];

        city = city == nil?@"":city;

        

        NSLog(@"你在%@%@%@",state,city,address);

        

        

    }];

    

}

 

 

@end

你可能感兴趣的:(Objective-C)