欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

復制代碼 代碼如下:

//
// 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];

// 取得經(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);

// 取得此時時刻
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); // 區(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)定義的字符串直接查詢,記得導入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 經(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); 
    } 
}]; 

地圖的使用以及標注地圖
使用CoreLocation框架獲取了當前設備的位置,這一章介紹地圖的使用。
首先,導入<MapKit.framework>框架:

復制代碼 代碼如下:

#import <MapKit/MapKit.h>

main代碼示例

復制代碼 代碼如下:

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 = @"地圖標注"; 
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)]; 
     
    //  是否顯示用戶當前位置 
    self.mapView.showsUserLocation = YES; 
    //  設置代理 
    self.mapView.delegate = self; 
     
    //  地圖顯示類型 
    /**
     *      MKMapTypeStandard = 0, //  標準地圖
     *      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}; 
    //  給地圖設置顯示區(qū)域 
    [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 = @"子標題"; 
    //  將標注添加到地圖上(執(zhí)行這步,就會執(zhí)行下面的代理方法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,當前位置會創(chuàng)建一個默認的標注視圖 
        return nil; 
    } 
     

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

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

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

 
//  地圖的現(xiàn)實區(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

復制代碼 代碼如下:

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 
//  引入MKAnnotation協(xié)議,切記不能忘記!!!!!!!!! 
@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開發(fā)之路--仿網(wǎng)易抽屜效果

    iOS開發(fā)之路--仿網(wǎng)易抽屜效果

    本文是IOS開發(fā)之路系列的第一篇,主要講訴了如何仿網(wǎng)易新聞客戶端實現(xiàn)抽屜效果,全部源代碼都分享給大家,希望對大家有所幫助
    2014-08-08
  • ios NSNotificationCenter通知的簡單使用

    ios NSNotificationCenter通知的簡單使用

    這篇文章主要介紹了ios NSNotificationCenter通知的簡單使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • android中UIColletionView瀑布流布局實現(xiàn)思路以及封裝的實現(xiàn)

    android中UIColletionView瀑布流布局實現(xiàn)思路以及封裝的實現(xiàn)

    本篇文章主要介紹了android中UIColletionView瀑布流布局實現(xiàn)思路以及封裝的實現(xiàn),具有一定的參考價值,有興趣的可以了解一下。
    2017-02-02
  • iOS中表單列表樣式鍵盤遮擋的解決方案

    iOS中表單列表樣式鍵盤遮擋的解決方案

    這篇文章主要給大家介紹了關于iOS中表單列表樣式鍵盤遮擋的解決方案,文中通過示例代碼將解決的方法一步步介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2019-01-01
  • 怎樣優(yōu)化今日頭條IOS安裝包

    怎樣優(yōu)化今日頭條IOS安裝包

    這篇文章主要介紹了怎樣優(yōu)化今日頭條IOS安裝包,對IOS優(yōu)化感興趣的同學,可以參考下
    2021-04-04
  • 講解iOS開發(fā)中基本的定位功能實現(xiàn)

    講解iOS開發(fā)中基本的定位功能實現(xiàn)

    這篇文章主要介紹了講解iOS開發(fā)中基本的定位功能實現(xiàn),示例基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-10-10
  • iOS實現(xiàn)簡易的導航欄顏色漸變實例代碼

    iOS實現(xiàn)簡易的導航欄顏色漸變實例代碼

    很多APP 都有導航欄顏色漸變的效果,下面這篇文章主要給大家介紹了關于iOS如何實現(xiàn)簡易的導航欄顏色漸變效果的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-10-10
  • iOS通過代理逆向傳值的方式詳解

    iOS通過代理逆向傳值的方式詳解

    在iOS開發(fā)中傳值是幾乎每個App都會用到的,對于傳統(tǒng)的順向傳值應該說是比較簡單的,但是逆向傳值往往會用到代理模式來實現(xiàn),很多同學在這一塊有迷惑,迷惑的不是怎么逆向傳值,而是不理解代理模式,下面這篇文章就給大家分析一下iOS通過代理逆向傳值的方式。
    2016-12-12
  • iOS實現(xiàn)獲取系統(tǒng)iTunes音樂的方法示例

    iOS實現(xiàn)獲取系統(tǒng)iTunes音樂的方法示例

    這篇文章主要給大家介紹了關于iOS如何實現(xiàn)獲取系統(tǒng)iTunes音樂的相關資料,文中通過示例代碼給大家詳細介紹了實現(xiàn)的方法,并給大家介紹了MPMediaPickerController的相關知識,對大家的學習或者工作具有一定的幫助,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-11-11
  • IOS 照片操作(獲取信息及修改照片)詳解

    IOS 照片操作(獲取信息及修改照片)詳解

    這篇文章主要介紹了IOS 照片操作及獲取拍照信息和修改信息的相關資料,這里主要介紹,獲取系統(tǒng)照片信息,及地理位置和時間的修改,需要的朋友可以參考下
    2016-11-11

最新評論