最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS開發(fā)中最基本的位置功能實(shí)現(xiàn)示例

 更新時(shí)間:2015年09月17日 09:19:01   作者:TommyYaphetS  
這篇文章主要介紹了iOS開發(fā)中最基本的位置功能實(shí)現(xiàn)示例,需要的朋友可以參考下

定位獲取位置及位置編碼-反編碼
我們的應(yīng)用程序,可以通過添加Core Location框架所包含的類,獲取設(shè)備的地圖位置。
添加CoreLocation.framework框架,導(dǎo)入#import<CoreLocation/CoreLocation.h>。
使用地圖服務(wù)時(shí),會(huì)消耗更多地設(shè)備電量.因此,在獲取到設(shè)備的位置后,應(yīng)該停止定位來節(jié)省電量。
我們通過一個(gè)demo來展示內(nèi)容與效果

復(fù)制代碼 代碼如下:

//
// 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{

// 初始化位置服務(wù)
self.locationManage = [[CLLocationManager alloc]init];

// 要求CLLocationManager對(duì)象返回全部信息
_locationManage.distanceFilter = kCLDistanceFilterNone;

// 設(shè)置定位精度
_locationManage.desiredAccuracy = kCLLocationAccuracyBest;

// 設(shè)置代理
_locationManage.delegate = self;

// 開始定位
[_locationManage startUpdatingLocation];

[_locationManage release];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

CLLocation * newLocation = [locations lastObject];
// 停止實(shí)時(shí)定位
[_locationManage stopUpdatingLocation];

// 取得經(jīng)緯度
CLLocationCoordinate2D coord2D = newLocation.coordinate;
double latitude = coord2D.latitude;
double longitude = coord2D.longitude;
NSLog(@"緯度 = %f 經(jīng)度 = %f",latitude,longitude);

// 取得精度
CLLocationAccuracy horizontal = newLocation.horizontalAccuracy;
CLLocationAccuracy vertical = newLocation.verticalAccuracy;
NSLog(@"水平方 = %f 垂直方 = %f",horizontal,vertical);

// 取得高度
CLLocationDistance altitude = newLocation.altitude;
NSLog(@"%f",altitude);

// 取得此時(shí)時(shí)刻
NSDate *timestamp = [newLocation timestamp];
// 實(shí)例化一個(gè)NSDateFormatter對(duì)象
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
// 設(shè)定時(shí)間格式
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
[dateFormat setAMSymbol:@"AM"]; // 顯示中文, 改成"上午"
[dateFormat setPMSymbol:@"PM"];
// 求出當(dāng)天的時(shí)間字符串,當(dāng)更改時(shí)間格式時(shí),時(shí)間字符串也能隨之改變
NSString *dateString = [dateFormat stringFromDate:timestamp];
NSLog(@"此時(shí)此刻時(shí)間 = %@",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); // 區(qū)
NSLog(@"country = %@",place.country); // 國家

NSArray *allKeys = place.addressDictionary.allKeys;
for (NSString *key in allKeys)
{
NSLog(@"key = %@, value = %@",key, place.addressDictionary[key]);
}
#pragma mark - 使用系統(tǒng)定義的字符串直接查詢,記得導(dǎo)入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

程序運(yùn)行結(jié)果:(以39.3,116.4為例)

復(fù)制代碼 代碼如下:

//  判斷輸入的地址 
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 經(jīng)度 = %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); 
    } 
}]; 

地圖的使用以及標(biāo)注地圖
使用CoreLocation框架獲取了當(dāng)前設(shè)備的位置,這一章介紹地圖的使用。
首先,導(dǎo)入<MapKit.framework>框架:

復(fù)制代碼 代碼如下:

#import <MapKit/MapKit.h>

main代碼示例

復(fù)制代碼 代碼如下:

main.h 
 
#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
//  引用地圖協(xié)議 
@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 = @"地圖標(biāo)注"; 
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)]; 
     
    //  是否顯示用戶當(dāng)前位置 
    self.mapView.showsUserLocation = YES; 
    //  設(shè)置代理 
    self.mapView.delegate = self; 
     
    //  地圖顯示類型 
    /**
     *      MKMapTypeStandard = 0, //  標(biāo)準(zhǔn)地圖
     *      MKMapTypeSatellite,    //  衛(wèi)星地圖
     *      MKMapTypeHybrid        //  混合地圖
     */ 
    self.mapView.mapType = MKMapTypeStandard; 
    //  經(jīng)緯度 
    CLLocationCoordinate2D coord2D = {39.910650,116.47030}; 
    //  顯示范圍,數(shù)值越大,范圍就越大 
    MKCoordinateSpan span = {0.1,0.1}; 
    //  顯示區(qū)域 
    MKCoordinateRegion region = {coord2D,span}; 
    //  給地圖設(shè)置顯示區(qū)域 
    [self.mapView setRegion:region animated:YES]; 
    //  是否允許縮放 
    //self.mapView.zoomEnabled = NO; 
    //  是否允許滾動(dòng) 
    //self.mapView.scrollEnabled = NO; 
 
    //  初始化自定義Annotation(可以設(shè)置多個(gè)) 
    HMTAnnotation *annotation = [[HMTAnnotation alloc] initWithCGLocation:coord2D]; 
    //  設(shè)置標(biāo)題 
    annotation.title = @"自定義標(biāo)注位置"; 
    //  設(shè)置子標(biāo)題 
    annotation.subtitle = @"子標(biāo)題"; 
    //  將標(biāo)注添加到地圖上(執(zhí)行這步,就會(huì)執(zhí)行下面的代理方法viewForAnnotation) 
    [self.mapView addAnnotation:annotation]; 
     
    [self.view addSubview:_mapView]; 
     

 
//   返回標(biāo)注視圖(大頭針視圖) 
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ 
 
    /**
     *  是不是有點(diǎn)像自定義UITableViewCell一樣
     */ 
    static NSString *identifier = @"annotation"; 
    //  復(fù)用標(biāo)注視圖(MKPinAnnotationView是大頭針視圖,繼承自MKAnnotation) 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 
    if (annotationView == nil) { 
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
    } 
    //  判斷是否為自定義的標(biāo)注視圖 
    if ([annotation isKindOfClass:[HMTAnnotation class]]) { 
         
        //  設(shè)置大頭針圓圈顏色 
        annotationView.pinColor = MKPinAnnotationColorGreen; 
        //  點(diǎn)擊頭針紅色圓圈是否顯示上面設(shè)置好的標(biāo)題視圖 
        annotationView.canShowCallout = YES; 
        //  要自定義錨點(diǎn)圖片,可考慮使用MKAnnotationView;MKPinAnnotationView只能是以大頭針形式顯示!?。?! 
        annotationView.image = [UIImage imageNamed:@"customImage"]; 
        //  添加標(biāo)題視圖右邊視圖(還有左邊視圖,具體可自行查看API) 
        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
        [button addTarget:self action:@selector(didClickAnnotationViewRightButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
        annotationView.rightCalloutAccessoryView = button; 
        //  是否以動(dòng)畫形式顯示標(biāo)注(從天而降) 
        annotationView.animatesDrop = YES; 
        annotationView.annotation = annotation; 
         
        //  返回自定義的標(biāo)注視圖 
        return annotationView; 
         
    }else{ 
        
        //  當(dāng)前設(shè)備位置的標(biāo)注視圖,返回nil,當(dāng)前位置會(huì)創(chuàng)建一個(gè)默認(rèn)的標(biāo)注視圖 
        return nil; 
    } 
     

 
- (void)didClickAnnotationViewRightButtonAction:(UIButton *)button{ 
 
    NSLog(@"%d %s",__LINE__,__FUNCTION__); 

 
//  更新當(dāng)前位置調(diào)用 
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ 
 
    NSLog(@"%d %s",__LINE__,__FUNCTION__); 

 
//  選中標(biāo)注視圖 
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
     
    NSLog(@"%d %s",__LINE__,__FUNCTION__); 

 
//  地圖的現(xiàn)實(shí)區(qū)域改變了調(diào)用 
- (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

復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
//  引入MKAnnotation協(xié)議,切記不能忘記!!!!!!!!! 
@interface HMTAnnotation : NSObject<MKAnnotation> 
 
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;  //  坐標(biāo) 
@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 

效果圖:

相關(guān)文章

  • 詳解IOS 單例的兩種方式

    詳解IOS 單例的兩種方式

    這篇文章主要介紹了詳解IOS 單例的兩種方式的相關(guān)資料,希望通過本文大家能夠理解掌握IOS 的兩種單例的使用方法,需要的朋友可以參考下
    2017-09-09
  • iOS中UIAlertController設(shè)置自定義標(biāo)題與內(nèi)容的方法

    iOS中UIAlertController設(shè)置自定義標(biāo)題與內(nèi)容的方法

    UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(雖然現(xiàn)在仍可以使用,但是會(huì)有警告)。下面這篇文章主要給大家介紹了關(guān)于iOS中UIAlertController如何設(shè)置自定義標(biāo)題與內(nèi)容的相關(guān)資料,需要的朋友可以參考下。
    2017-10-10
  • ios啟動(dòng)頁強(qiáng)制豎屏(進(jìn)入App后允許橫屏與豎屏)

    ios啟動(dòng)頁強(qiáng)制豎屏(進(jìn)入App后允許橫屏與豎屏)

    最近工作遇到這樣一個(gè)需要,當(dāng)進(jìn)入啟動(dòng)頁需要強(qiáng)制豎屏,而進(jìn)入APP后就允許橫屏與豎屏,通過查找相關(guān)的資料找到了解決的方法,所以將實(shí)現(xiàn)的方法整理后分享出來,需要的朋友們可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • 詳解iOS開發(fā)獲取當(dāng)前控制器的正取方式

    詳解iOS開發(fā)獲取當(dāng)前控制器的正取方式

    這篇文章主要介紹了iOS開發(fā)獲取當(dāng)前控制器的正取方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • iOS的CoreAnimation開發(fā)框架中的Layer層動(dòng)畫制作解析

    iOS的CoreAnimation開發(fā)框架中的Layer層動(dòng)畫制作解析

    在iOS中UIView層的屬性會(huì)映射到CoreAnimation框架的CALayer,這里我們來看一下iOS的CoreAnimation開發(fā)框架中的Layer層動(dòng)畫制作解析,需要的朋友可以參考下
    2016-07-07
  • 適配iPhoneXS max和iPhoneX R的方法示例

    適配iPhoneXS max和iPhoneX R的方法示例

    這篇文章主要介紹了適配iPhoneXS max和iPhoneX R的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • iOS實(shí)現(xiàn)小型計(jì)算器

    iOS實(shí)現(xiàn)小型計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)小型計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • IOS開發(fā)Swift?與?OC相互調(diào)用詳解

    IOS開發(fā)Swift?與?OC相互調(diào)用詳解

    這篇文章主要為大家介紹了IOS開發(fā)Swift?與?OC相互調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • iOS開發(fā)教程之扇形動(dòng)畫的實(shí)現(xiàn)

    iOS開發(fā)教程之扇形動(dòng)畫的實(shí)現(xiàn)

    實(shí)現(xiàn)扇形圖大家應(yīng)該都會(huì)的,但是扇形動(dòng)畫大家都會(huì)實(shí)現(xiàn)嗎?下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之扇形動(dòng)畫實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06
  • iOS編寫下拉刷新控件

    iOS編寫下拉刷新控件

    這篇文章主要介紹了iOS編寫下拉刷新控件的相關(guān)資料,iOS如何寫個(gè)普通的下拉刷新的控件,需要了解的朋友可以參考下文
    2016-04-04

最新評(píng)論

泸西县| 五大连池市| 巫溪县| 抚松县| 台安县| 合阳县| 陆丰市| 琼结县| 铅山县| 肇庆市| 定安县| 南木林县| 拜泉县| 台中市| 梧州市| 霍邱县| 科技| 逊克县| 禹城市| 晋城| 保靖县| 普定县| 开封县| 界首市| 怀集县| 达日县| 蒙城县| 密云县| 临江市| 突泉县| 台南市| 金堂县| 共和县| 瓮安县| 鄱阳县| 革吉县| 西昌市| 靖安县| 屯门区| 旺苍县| 什邡市|