定位獲得地位及地位編碼-反編碼
我們的運用法式,可以經由過程添加Core Location框架所包括的類,獲得裝備的地圖地位。
添加CoreLocation.framework框架,導入#import<CoreLocation/CoreLocation.h>。
應用地圖辦事時,會消費更多地裝備電量.是以,在獲得到裝備的地位後,應當停滯定位來節儉電量。
我們經由過程一個demo來展現內容與後果
//
// HMTRootViewController.h
// My-GPS-Map
//
// Created by hmt on 14-4-12.
// Copyright (c) 2014年 胡明濤. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface HMTRootViewController : UIViewController <CLLocationManagerDelegate>
@end
//
// HMTRootViewController.m
// My-GPS-Map
//
// Created by hmt on 14-4-12.
// Copyright (c) 2014年 胡明濤. All rights reserved.
//
#import "HMTRootViewController.h"
#import <AddressBook/AddressBook.h>
@interface HMTRootViewController (){
CLLocationManager * _locationManage;
}
@property (nonatomic,retain) CLLocationManager * locationManage;
@end
@implementation HMTRootViewController
- (void)dealloc{
RELEASE_SAFELY(_locationManage);
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createGPSMap];
self.view.backgroundColor = [UIColor redColor];
}
- (void)createGPSMap{
// 初始化地位辦事
self.locationManage = [[CLLocationManager alloc]init];
// 請求CLLocationManager對象前往全體信息
_locationManage.distanceFilter = kCLDistanceFilterNone;
// 設置定位精度
_locationManage.desiredAccuracy = kCLLocationAccuracyBest;
// 設置署理
_locationManage.delegate = self;
// 開端定位
[_locationManage startUpdatingLocation];
[_locationManage release];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation * newLocation = [locations lastObject];
// 停滯及時定位
[_locationManage stopUpdatingLocation];
// 獲得經緯度
CLLocationCoordinate2D coord2D = newLocation.coordinate;
double latitude = coord2D.latitude;
double longitude = coord2D.longitude;
NSLog(@"緯度 = %f 經度 = %f",latitude,longitude);
// 獲得精度
CLLocationAccuracy horizontal = newLocation.horizontalAccuracy;
CLLocationAccuracy vertical = newLocation.verticalAccuracy;
NSLog(@"程度方 = %f 垂直方 = %f",horizontal,vertical);
// 獲得高度
CLLocationDistance altitude = newLocation.altitude;
NSLog(@"%f",altitude);
// 獲得此不時刻
NSDate *timestamp = [newLocation timestamp];
// 實例化一個NSDateFormatter對象
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
// 設准時間格局
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
[dateFormat setAMSymbol:@"AM"]; // 顯示中文, 改成"上午"
[dateFormat setPMSymbol:@"PM"];
// 求出當天的時光字符串,當更改時光格局時,時光字符串也能隨之轉變
NSString *dateString = [dateFormat stringFromDate:timestamp];
NSLog(@"此時此刻時光 = %@",dateString);
// -----------------------------------------地位反編碼--------------------------------------------
CLGeocoder * geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * place in placemarks) {
NSLog(@"name = %@",place.name); // 地位名
NSLog(@"thoroughfare = %@",place.thoroughfare); // 街道
NSLog(@"subAdministrativeArea = %@",place.subAdministrativeArea); // 子街道
NSLog(@"locality = %@",place.locality); // 市
NSLog(@"subLocality = %@",place.subLocality); // 區
NSLog(@"country = %@",place.country); // 國度
NSArray *allKeys = place.addressDictionary.allKeys;
for (NSString *key in allKeys)
{
NSLog(@"key = %@, value = %@",key, place.addressDictionary[key]);
}
#pragma mark - 應用體系界說的字符串直接查詢,記得導入AddressBook框架
NSLog(@"kABPersonAddressCityKey = %@", (NSString *)kABPersonAddressCityKey);
NSLog(@"city = %@", place.addressDictionary[(NSString *)kABPersonAddressCityKey]);
NSString *city = place.locality;
if(city == nil)
{
city = place.addressDictionary[(NSString *)kABPersonAddressStateKey];
}
}
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
法式運轉成果:(以39.3,116.4為例)
// 斷定輸出的地址
if (self.locationTextField.text == nil || [self.locationTextField.text length] == 0) {
return;
}
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
/* -----------------------------------------地位編碼-------------------------------------------- */
[geocoder geocodeAddressString:_locationTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *placemark in placemarks) {
CLLocationCoordinate2D coordinate = placemark.location.coordinate;
NSString *strCoordinate = [NSString stringWithFormat:@"緯度 = %3.5f\n 經度 = %3.5f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@",strCoordinate);
NSDictionary *addressDictionary = placemark.addressDictionary;
NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *state = [addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];
NSLog(@"街道 = %@\n 省 = %@\n 城市 = %@",address,state,city);
}
}];
地圖的應用和標注地圖
應用CoreLocation框架獲得了以後裝備的地位,這一章引見地圖的應用。
起首,導入<MapKit.framework>框架:
#import <MapKit/MapKit.h>
main代碼示例
main.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
// 援用地圖協定
@interface HMTMainViewController : UIViewController<MKMapViewDelegate>
@end
main.m
//
// HMTMainViewController.m
// Map
//
// Created by HMT on 14-6-21.
// Copyright (c) 2014年 humingtao. All rights reserved.
//
#import "HMTMainViewController.h"
#import "HMTAnnotation.h"
@interface HMTMainViewController ()
@property (nonatomic ,strong) MKMapView *mapView;
@end
@implementation HMTMainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
// Do any additional setup after loading the view.
self.navigationItem.title = @"地圖標注";
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
// 能否顯示用戶以後地位
self.mapView.showsUserLocation = YES;
// 設置署理
self.mapView.delegate = self;
// 地圖顯示類型
/**
* MKMapTypeStandard = 0, // 尺度地圖
* MKMapTypeSatellite, // 衛星地圖
* MKMapTypeHybrid // 混雜地圖
*/
self.mapView.mapType = MKMapTypeStandard;
// 經緯度
CLLocationCoordinate2D coord2D = {39.910650,116.47030};
// 顯示規模,數值越年夜,規模就越年夜
MKCoordinateSpan span = {0.1,0.1};
// 顯示區域
MKCoordinateRegion region = {coord2D,span};
// 給地圖設置顯示區域
[self.mapView setRegion:region animated:YES];
// 能否許可縮放
//self.mapView.zoomEnabled = NO;
// 能否許可轉動
//self.mapView.scrollEnabled = NO;
// 初始化自界說Annotation(可以設置多個)
HMTAnnotation *annotation = [[HMTAnnotation alloc] initWithCGLocation:coord2D];
// 設置題目
annotation.title = @"自界說標注地位";
// 設置子題目
annotation.subtitle = @"子題目";
// 將標注添加到地圖上(履行這步,就會履行上面的署理辦法viewForAnnotation)
[self.mapView addAnnotation:annotation];
[self.view addSubview:_mapView];
}
// 前往標注目圖(年夜頭針視圖)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
/**
* 是否是有點像自界說UITableViewCell一樣
*/
static NSString *identifier = @"annotation";
// 復用標注目圖(MKPinAnnotationView是年夜頭針視圖,繼續自MKAnnotation)
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
// 斷定能否為自界說的標注目圖
if ([annotation isKindOfClass:[HMTAnnotation class]]) {
// 設置年夜頭針圓圈色彩
annotationView.pinColor = MKPinAnnotationColorGreen;
// 點擊頭針白色圓圈能否顯示下面設置好的題目視圖
annotationView.canShowCallout = YES;
// 要自界說錨點圖片,可斟酌應用MKAnnotationView;MKPinAnnotationView只能是以年夜頭針情勢顯示!!!!
annotationView.image = [UIImage imageNamed:@"customImage"];
// 添加題目視圖左邊視圖(還有右邊視圖,詳細可自行檢查API)
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:@selector(didClickAnnotationViewRightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = button;
// 能否以動畫情勢顯示標注(突如其來)
annotationView.animatesDrop = YES;
annotationView.annotation = annotation;
// 前往自界說的標注目圖
return annotationView;
}else{
// 以後裝備地位的標注目圖,前往nil,以後地位會創立一個默許的標注目圖
return nil;
}
}
- (void)didClickAnnotationViewRightButtonAction:(UIButton *)button{
NSLog(@"%d %s",__LINE__,__FUNCTION__);
}
// 更新以後地位挪用
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"%d %s",__LINE__,__FUNCTION__);
}
// 選中標注目圖
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(@"%d %s",__LINE__,__FUNCTION__);
}
// 地圖的實際區域轉變了挪用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"%d %s",__LINE__,__FUNCTION__);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
自界說MKAnnotationView
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
// 引入MKAnnotation協定,切記不克不及忘卻!!!!!!!!!
@interface HMTAnnotation : NSObject<MKAnnotation>
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate; // 坐標
@property (nonatomic,copy) NSString *title; // 地位稱號
@property (nonatomic,copy) NSString *subtitle; // 地位子信息(可選)
- (id)initWithCGLocation:(CLLocationCoordinate2D) coordinate;
@end
#import "HMTAnnotation.h"
@implementation HMTAnnotation
- (id)initWithCGLocation:(CLLocationCoordinate2D)coordinate{
if (self = [super init]) {
_coordinate = coordinate;
}
return self;
}
@end
後果圖:
【iOS開辟中最根本的地位功效完成示例】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!