美文网首页
SYCLLocation定位与反编码

SYCLLocation定位与反编码

作者: 番薯大佬 | 来源:发表于2017-10-17 14:45 被阅读5次

    SYCLLocation使用系统地图进行定位及反编码(二次封装类,便于开发)。

    效果图1效果图1
    效果图2效果图2

    代码示例

    // 导入封装类头文件
    #import "SYCLLocation.h"
    
    // 封装方法 开启定位
    [[SYCLLocation shareLocation] locationStart:^(CLLocation *location, CLPlacemark *placemark) {
        NSString *name = placemark.name;
        NSString *thoroughfare = placemark.thoroughfare;
        NSString *subThoroughfare = placemark.subThoroughfare;
        NSString *subLocality = placemark.subLocality;
        NSString *administrativeArea = placemark.administrativeArea;
        NSString *subAdministrativeArea = placemark.subAdministrativeArea;
        NSString *postalCode = placemark.postalCode;
        NSString *ISOcountryCode = placemark.ISOcountryCode;
        NSString *country = placemark.country;
        NSString *inlandWater = placemark.inlandWater;
        NSString *ocean = placemark.ocean;
        NSArray *areasOfInterest = placemark.areasOfInterest;
        // 获取城市
        NSString *city = placemark.locality;
        if (!city)
        {
            // 四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
            city = placemark.administrativeArea;
        }
    
        NSMutableString *text = [[NSMutableString alloc] initWithFormat:@"纬度=%f,经度=%f\n", location.coordinate.latitude, location.coordinate.longitude];
        [text appendFormat:@"city=%@\n", city];
        [text appendFormat:@"name=%@\n", name];
        [text appendFormat:@"thoroughfare=%@\n", thoroughfare];
        [text appendFormat:@"subThoroughfare=%@\n", subThoroughfare];
        [text appendFormat:@"subLocality=%@\n", subLocality];
        [text appendFormat:@"administrativeArea=%@\n", administrativeArea];
        [text appendFormat:@"subAdministrativeArea=%@\n", subAdministrativeArea];
        [text appendFormat:@"postalCode=%@\n", postalCode];
        [text appendFormat:@"ISOcountryCode=%@\n", ISOcountryCode];
        [text appendFormat:@"country=%@\n", country];
        [text appendFormat:@"inlandWater=%@\n", inlandWater];
        [text appendFormat:@"inlandWater=%@\n", inlandWater];
        [text appendFormat:@"ocean=%@\n", ocean];
        [text appendFormat:@"areasOfInterest=%@\n", areasOfInterest];
        NSLog(@"%@", text);
    } faile:^(NSError *error) {
        if (error)
        {
            if (([error code] == kCLErrorDenied))
            {
                NSLog(@"定位未打开,请打开定位服务");
            }
            else
            {
                NSLog(@"An error occurred = %@", error);
            }
        }
        else
        {
            NSLog(@"No results were returned.");
        }
    }];
    
    // 封装方法 结束定位
    [[SYCLLocation shareLocation] locationStop];
    

    注意事项

    • 添加CoreLocation.framework
    • 导入头文件
      • /#import <CoreLocation/CoreLocation.h>
      • /#import <CoreLocation/CLLocationManager.h>
    • plist文件中设置定位的私有属性
      • NSLocationAlwaysUsageDescription 设置为 YES
      • NSLocationWhenInUseUsageDescription 设置为 YES
      • NSLocationUsageDescription 设置提示语,如:想知道你在哪里?
    plist文件配置示意图plist文件配置示意图

    相关文章

      网友评论

          本文标题:SYCLLocation定位与反编码

          本文链接:https://www.haomeiwen.com/subject/bmjduxtx.html