最近プロジェクトをする時地図を使って、そこで自分で1つのクラスをカプセル化して、地図のいくつかのよく使う方法について、符号化、逆符号化、位置決めなど
8708 ワード
//
// GetLocation.h
//
//
// Created by mac on 15/12/25.
// Copyright (c) 2015 ZY. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface GetLocation : NSObject<CLLocationManagerDelegate>
@property (nonatomic,strong)CLLocation *location;
@property (nonatomic,strong)CLGeocoder *geocoder;
@property (nonatomic,strong)CLLocationManager *manager;
@property (nonatomic,copy)void(^LBlock)(NSString *cityName);
+ (instancetype)shareInstanceType;
- (void)getCurrentLocation;
@end
//
// GetLocation.m
//
//
// Created by mac on 15/12/25.
// Copyright (c) 2015 ZY. All rights reserved.
//
#import "GetLocation.h"
@implementation GetLocation
+ (instancetype)shareInstanceType{
static GetLocation *getLo = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
getLo = [[[self class] alloc] init];
});
return getLo;
}
- (CLLocationManager *)manager{
if (!_manager) {
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
}
return _manager;
}
- (CLLocation *)location{
if (!_location) {
_location = [[CLLocation alloc] init];
}
return _location;
}
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}
#pragma mark
- (void)getCurrentLocation{
//
BOOL isOpen = [CLLocationManager locationServicesEnabled];
if (isOpen) {
NSLog(@" ");
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" " message:@" " delegate:self cancelButtonTitle:@" " otherButtonTitles:nil, nil];
[alert show];
return;
}
//
NSInteger status = [CLLocationManager authorizationStatus];
if (status == 0) {
//
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
//
//
// [self.manager requestAlwaysAuthorization];
//
[self.manager requestWhenInUseAuthorization];
// info.plist
//NSLocationAlwaysUsageDescription---
//NSLocationWhenInUseUsageDescription--- app , ?
}
}else if (status == 2){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" " message:@" , " delegate:self cancelButtonTitle:@" " otherButtonTitles:nil, nil];
[alert show];
return;
}
//
//
self.manager.desiredAccuracy = kCLLocationAccuracyBest;
//
self.manager.distanceFilter = 100.f;
//
[self.manager startUpdatingLocation];
}
#pragma mark
- (void)getCityNameUseLatitude:(double)latitude withLongitude:(double)longitude{
_location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[self.geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@" ==%@",error);
}else{
//
CLPlacemark *pm = [placemarks firstObject];
// country:
//administrativeArea:
//locality:
//subLocality:
//thoroughfare:
//subThoroughfare:
NSLog(@"%@",[NSString stringWithFormat:@"%@-%@-%@-%@-%@-%@",pm.country,pm.administrativeArea,pm.location,pm.subLocality,pm.thoroughfare,pm.subThoroughfare]);
NSLog(@" :%ld",(unsigned long)placemarks.count);
for (CLPlacemark *place in placemarks) {
// NSLog(@"======");
[place.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if(_LBlock){
_LBlock(obj);
}
NSLog(@"%@:%@",key,obj);
}];
// NSLog(@"=====");
}
}
}];
}
#pragma mark
- (void)getCLLocationCoordinateWithCityName:(NSString *)cityName{
[self.geocoder geocodeAddressString:cityName completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@" ");
}
//
CLPlacemark *pm = [placemarks firstObject];
//
CLLocationCoordinate2D coor = pm.location.coordinate;
NSLog(@" :%.2f
:%.2f",coor.longitude,coor.latitude);
NSLog(@" :%ld",(unsigned long)placemarks.count);
for (CLPlacemark *place in placemarks) {
[place.addressDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"%@:%@",key,obj);
}];
}
}];
}
#pragma mark CLLocationDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *loaction = [locations firstObject];
// CLLocationCoordinate2D:
/*typedef struct {
CLLocationDegrees latitude;
CLLocationDegrees longitude;
} CLLocationCoordinate2D;*/
CLLocationCoordinate2D coor = loaction.coordinate;
NSLog(@"%.2f %.2f",coor.latitude,coor.longitude);
[self getCityNameUseLatitude:coor.latitude withLongitude:coor.longitude];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
NSLog(@"---%d",status);
switch (status) {
case 0:
NSLog(@" ");
break;
default:
break;
}
}
//
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region{
NSLog(@"%s",__FUNCTION__);
}
//
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(@"%s",__FUNCTION__);
}
//
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(@"%s",__FUNCTION__);
}
@end