zoukankan      html  css  js  c++  java
  • iOS定位服务与地图开发(3)---地理信息编码查询

    即根据一个NSString的文字描述对象获取相关的地理坐标。

    采用CLGeocoder类操作,具体方法:

    1>geocodeAddressDictionary:completionHandler: 通过指定一个地址信息字典对象参数进行查询

    2>geocodeAddressString:completionHandler:通过指定一个地址信息字符串参数进行查询

    3>geocodeAddressString:inRegion:completionHandler:通过制定地址信息字符串和查询范围进行查询

    - (IBAction)geocodeQuery:(id)sender
    {
        NSString *queryStr = nil;
        // 从界面文本框输入地址字符串
        if (_textField.text == nil || [_textField.text length] == 0) {
            return ;
        }
        
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:queryStr completionHandler:^(NSArray *placemarks, NSError *error) {
            NSLog(@"查询记录数:%i",[placemarks count]);
            if ([placemarks count] > 0) {
                
                CLPlacemark *placemark = [placemarks objectAtIndex:0];
                CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                
                // 生成经纬度字符串
                NSString *strCoordinate = [NSString stringWithFormat:@"经度:%3.5f,纬度:%3.5f",coordinate.latitude,coordinate.longitude];
                
                NSDictionary *addressDict = placemark.addressDictionary;
                
                NSString *address = [addressDict objectForKey:(NSString *)kABPersonAddressStreetKey];
                address = address == nil ? @"" : address;
                
                NSString *state = [addressDict objectForKey:(NSString *)kABPersonAddressStateKey];
                
                state = state == nil ? @"" : state;
                
                NSString *city = [addressDict objectForKey:(NSString *)kABPersonAddressCityKey];
                
                city = city == nil ? @"" : city;
                
                //  获取地标对应的可读的文字信息,一般返回到UI显示
                NSString *streetInfo = [NSString stringWithFormat:@"%@
    %@
    %@
    %@
    ",strCoordinate,state,address,city];
            }
        }];
    }

    使用geocodeAddressString:inRegion:completionHandler:指定查询区域:

     CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:_currLocation.coordinate radius:1000.0f identifier:@"GeocodeRegion"];
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:queryStr inRegion:region completionHandler:^(NSArray *placemarks, NSError *error) {
            .......
        }];

    CLRegion对象封装了一个地理区域类。

    构造方法参数:

    center:指定区域中心点

    radius:指定区域半径,单位为米

    identifier:为区域指定一个标识

  • 相关阅读:
    Cross-Site Scripting XSS 跨站攻击全攻略 分类: 系统架构 2015-07-08 12:25 21人阅读 评论(2) 收藏
    WAS集群:记一次Node Agent不活动问题解决过程
    Oracle RAC集群资料收集
    Linux使用 tar命令-g参数进行增量+差异备份、还原文件
    WAS7.0安装补丁升级程序无法替换文件 java/docs/autorun.inf解决办法
    Java程序员面试失败的5大原因
    Lemon OA第4篇:常用功能
    Lemon OA第3篇:核心功能
    Lemon OA第2篇:功能解析方法
    Activiti5小试牛刀demo流程
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3721799.html
Copyright © 2011-2022 走看看