iOS开发笔记(1) 根据经纬度翻译成详细位置的各种方法

本文介绍iOS设备如何通过CLLocationManager获取地理位置信息,并实现经纬度到地址的解析。同时探讨了两种解析方法:使用系统CLGeocoder类及调用Google API。此外还提供了查找附近的实用API示例。

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

首先苹果获取经纬度是

    if ([CLLocationManager locationServicesEnabled]) {//判断手机是否可以定位
        locationManager = [[CLLocationManager alloc] init];//初始化位置管理器
        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//设置精度
        locationManager.distanceFilter = 1000.0f;//设置距离筛选器
        [locationManager startUpdatingLocation];//启动位置管理器
    }
然后根据其代理获取经纬度

#pragma mark - CLLocationManager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;
{
    location=[newLocation coordinate];//当前经纬度
    lat=location.latitude;
    lon=location.longitude;
    
    MKCoordinateSpan theSpan;
    theSpan.latitudeDelta=0.01f;
    theSpan.longitudeDelta=0.01f;
    theRegion.center=location;//定义地图显示范围
    theRegion.span=theSpan;
}
根据经纬度解析成位置目前有2种。第一个是调用系统的CLGeocoder类的 reverseGeocodeLocation方法

 //根据经纬度解析成位置
    CLGeocoder *geocoder=[[[CLGeocoder alloc]init]autorelease];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark,NSError *error)
     {
         CLPlacemark *mark=[placemark objectAtIndex:0];
         place.title=@"没有当前位置的详细信息";
         place.subTitle=@"详细信息请点击‘附近’查看";
         place.title=[NSString stringWithFormat:@"%@%@%@",mark.subLocality,mark.thoroughfare,mark.subThoroughfare];
         place.subTitle=[NSString stringWithFormat:@"%@",mark.name];//获取subtitle的信息
         [self.myMapView selectAnnotation:place animated:YES];
     } ];
第二种是调用google的api

----http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true

网上有人说系统的方法有时候解析不出来位置。第二种google,国内有时候访问不了google服务器,所以都有缺点。

有人建议国内的地图可以使用高德地图(以后摸索下)

PS1:网上有一个比较流行的查找附近功能,是根据google的api搜索的

#define SEARCH_GOOGLE @"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=1000&types=%@&sensor=true&key=AIzaSyALaqx0MfPsp2aldbZbzEQAq64SwgQfZ0c"


调用的话就是这样

jsonString = [NSString stringWithFormat:SEARCH_GOOGLE,location.coordinate.latitude,location.coordinate.longitude,@"food"];

jsonString = [NSString stringWithFormat:SEARCH_GOOGLE,location.coordinate.latitude,location.coordinate.longitude,@"bus_station"];

jsonString = [NSString stringWithFormat:SEARCH_GOOGLE,location.coordinate.latitude,location.coordinate.longitude,@"hospital"];


PS2:有个功能,如果长时间按住地图上某个地方,如何获取其经纬度,进而对其显示位置呢?

----

    //开始点击长按,不然会执行2次。
    if (sender.state==UIGestureRecognizerStateBegan) {
        //isLongPress=YES;
        CGPoint touchPoint=[sender locationInView:self.myMapView];
        CLLocationCoordinate2D touchCoordinate=[self.myMapView convertPoint:touchPoint toCoordinateFromView:self.myMapView];//将触摸点转换经纬度
        //反向解析长按时的大头针的信息
        location =[[CLLocation alloc]initWithLatitude:touchCoordinate.latitude longitude:touchCoordinate.longitude];
 }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值