ios 定位工具类

//

//  LocationTool.m

//  QduniversityJobhelper

//

//  Created by niuxinghua on 14-12-21.

//  Copyright (c) 2014 niuxinghua. All rights reserved.

//


#import "LocationTool.h"


@implementation LocationTool

-(NSString*)locate{

   

    if([CLLocationManager locationServicesEnabled]) {

        _manager = [[CLLocationManager alloc] init];

        

        self.manager.delegate = self;

    }else {

        //提示用户无法进行定位操作

        NSLog(@"无法定位。。。");

    }

    

    // 开始定位

    [_manager startUpdatingLocation];

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

    //根据经纬度反向地理编译出地址信息

    [geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *array, NSError *error)

     {

         if (array.count > 0)

         {

             CLPlacemark *placemark = [array objectAtIndex:0];

             //获取城市

             _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];

    return _city;

}

    





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

{

    //此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation

    CLLocation *currentLocation = [locations lastObject];

    

    CLLocationCoordinate2D coor = currentLocation.coordinate;

    self.latitude =  coor.latitude;

    self.longitude = coor.longitude;

    

    //[self.locationManager stopUpdatingLocation];

    

}

@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值