iOS8 根据经纬度显示地名

本文介绍了一个iOS8应用中利用手机获取用户地理位置的方法。通过调用CoreLocation框架和MapKit框架,实现从经纬度到具体地址的转换。该示例包括了如何请求权限、更新位置信息及将经纬度转换为地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们很多时候做项目的时候都选用通过手机获取定位,然后传数据给后台,这是iOS8根据经纬度显示地名的demo

//1.导入框架

//

//  NearByViewController.m

//  nearDemo

//

//  Created by apple on 14/12/5.

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

//

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MapKit.h>

@interface NearByViewController ()<UITableViewDataSource,UITableViewDelegate,CLLocationManagerDelegate,DataRequestManagerDelegate>


{

    NSUserDefaults *defaluts;

    NSString * jingduStr;

    NSString *weiduStr;

    NSString *LocationAddress;

}

@property(nonatomic,retain)CLLocationManager *locationManager;

@property(strong,nonatomic)UILabel *positionLabel;

/*

  经纬度相关设置

 

 */


-(void)getLocationAll

{


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

    _locationManager.delegate = self;

    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    _locationManager.distanceFilter = 10;

    [_locationManager requestAlwaysAuthorization];//添加这句

    [_locationManager startUpdatingLocation];

   

}

/*

//通过经纬度得到当前地理位置

-(void)change{

    defaluts=[NSUserDefaults standardUserDefaults];


    CLLocationCoordinate2D coordinate;

    coordinate.latitude =  [[NSString stringWithFormat:@"%@",jingduStr] floatValue];

    coordinate.longitude = [[NSString stringWithFormat:@"%@",weiduStr] floatValue];


    CLLocation *newLocation=[[CLLocation alloc]initWithLatitude:coordinate.latitude longitude: coordinate.longitude];

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

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

     {

         MKPlacemark*placemark = [placemarks objectAtIndex:0];

                  LocationAddress = [NSString stringWithFormat:@"%@%@%@%@",  placemark.locality, placemark.subLocality,placemark.thoroughfare, placemark.subThoroughfare];

                  [defaluts setObject:LocationAddress forKey:@"address"];

                 NSLog(@"终极目标:1:%@2:%@3:%@4:%@",  placemark.locality, placemark.subLocality,placemark.thoroughfare,placemark.subThoroughfare);


     }];


#pragma mark--实现CLLocationManagerDelegate的代理方法,获取地理位置(镔)===========================================================================


//(1)获取到位置数据,返回的是一个CLLocation的数组,一般使用其中的一个


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

{

    CLLocation *currLocation = [locations lastObject];

    NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);

    jingduStr =[NSString stringWithFormat:@"%f", currLocation.coordinate.latitude];

    weiduStr =[NSString stringWithFormat:@"%f", currLocation.coordinate.longitude];

    NSLog(@"jijijijijij:%@",jingduStr);

     [self change];


    }

//(2)获取用户位置数据失败的回调方法,在此通知用户

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    if ([error code] == kCLErrorDenied)

    {

        //访问被拒绝

    }

    if ([error code] == kCLErrorLocationUnknown) {

        //无法获取位置信息

    }

}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值