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

  • 相关阅读:
    前端笔记之React(五)Redux深入浅出
    前端笔记之React(四)生命周期&Virtual DOM和Diff算法&日历组件开发
    前端笔记之React(三)使用动态样式表&antd&React脚手架&props实战
    前端笔记之React(二)组件内部State&React实战&表单元素的受控
    前端笔记之React(一)初识React&组件&JSX语法
    详解Asp.net MVC DropDownLists
    String.Format格式说明
    jquery日历datepicker的使用方法
    asp.net文本编辑器(FCKeditor)
    将jira添加至开机自启动
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3721799.html
Copyright © 2011-2022 走看看