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:为区域指定一个标识

  • 相关阅读:
    MySQL自定义函数 1418报错
    MySQL存储过程查询
    MySQL存储过程---游标
    MySQL存储过程---流程控制(循环)
    MySQL存储过程---流程控制(分支)
    设计模式——单例模式
    准备写一个 四川票务网的 检测票自动买汽车票功能,结果登录不上悲伤,继续研究
    python批量下载微信好友头像,微信头像批量下载
    arduino 522样本中文注释
    zabbix服务的部署
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3721799.html
Copyright © 2011-2022 走看看