taiyang2014

摇一摇定位

1.引入框架 CoreLocation.framework
2. 在info中 添加NSLocationWhenInUseUsageDescription //弹出位置允许
3.引入头文件
 
//  ViewController.m
//  LocationShark
//
//  Created by su on 15/12/4.
//  Copyright © 2015年 tian. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController ()<CLLocationManagerDelegate>
{
    //摇一摇相关
    CLLocationManager *locationManager;
    CLGeocoder *_geo;
    double lon;
    double lat;
}
@property (weak, nonatomic) IBOutlet UILabel *lab;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //摇一摇
    //_locationManager = [[CLLocationManager alloc] init];
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
   [self becomeFirstResponder];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = 1000;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [locationManager requestWhenInUseAuthorization];  //调用了这句,就会弹出允许框了.
    }
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0)
    {
        //设置定位权限 仅ios8有意义
        [locationManager requestWhenInUseAuthorization];// 前台定位
        //  [locationManager requestAlwaysAuthorization];// 前后台同时定位
    }
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    SystemSoundID soundID;
    //需要关闭ARC
    //    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
    //进行播放音效
    AudioServicesPlaySystemSound(soundID);
    //短期震动
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
   
    //检测到摇动
   
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //摇动结束
    if (event.subtype == UIEventSubtypeMotionShake) {
        //something happens
        [self test];
    }
}
-(void)test{
    [locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    NSLog(@"didChangeAuthorizationStatus---%u",status);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"didChangeAuthorizationStatus----%@",error);
}
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    CLLocation*  newLocation = [locations lastObject];
   
    [locationManager stopUpdatingLocation];
    lon = newLocation.coordinate.longitude;
    lat = newLocation.coordinate.latitude;
    [locationManager stopUpdatingLocation];
   
    CLLocationCoordinate2D coordinate = newLocation.coordinate;
    NSLog(@"输出当前的精度和纬度");
    NSLog(@"精度:%f 纬度:%f",coordinate.latitude,coordinate.longitude);
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error)
     {
         if (array.count > 0)
         {
             CLPlacemark *placemark = [array objectAtIndex:0];
             //将获得的所有信息显示到label上
             NSString * str = placemark.name;
             NSLog(@"~~~~~%@!~~~~~~",str);
             self.lab.text =[NSString stringWithFormat:@"%@",placemark.name];
             NSLog(@"name is %@",placemark.name);
            
             NSLog(@"thoroughfare is %@",placemark.thoroughfare);
            
             NSLog(@"subThoroughfare is %@",placemark.subThoroughfare);
             NSLog(@"locality is %@",placemark.locality);//沈阳市
             NSLog(@"subLocality is %@",placemark.subLocality);//东陵区
             NSLog(@"administrativeArea is %@",placemark.administrativeArea);//辽宁省
             NSLog(@"subAdministrativeArea is %@",placemark.subAdministrativeArea);
             NSLog(@"postalCode is %@",placemark.postalCode);
             NSLog(@"ISOcountryCode is %@",placemark.ISOcountryCode);
             NSLog(@"country is %@",placemark.country);
             NSLog(@"inlandWater is %@",placemark.inlandWater);
             NSLog(@"ocean is %@",placemark.ocean);
             NSLog(@"areasOfInterest is %@",placemark.areasOfInterest);
             NSString *detailaddressstr = @"";
             NSRange range;
             range = [placemark.name rangeOfString:@"区"];
             if (range.location != NSNotFound) {
                 detailaddressstr = [placemark.name substringFromIndex:range.location+1];
             }
             else
             {
                 detailaddressstr = placemark.name;
             }
 
             NSString *strProvince = @"省";
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                 city = placemark.administrativeArea;
             }
             NSLog(@"city = %@", city);
         }
         else if (error == nil && [array count] == 0)
         {
             NSLog(@"No results were returned.");
         }
         else if (error != nil)
         {
             NSLog(@"An error occurred = %@", error);
         }
     }];
    //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
    [manager stopUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
 

posted on 2015-12-04 18:23  taiyang2014  阅读(331)  评论(0编辑  收藏  举报

导航