地理
2019獨角獸企業重金招聘Python工程師標準>>>
//
//? ViewController.m
//? Core Location_定位功能
//
//? Created by DC020 on 15/12/23.
//? Copyright (c) 2015年 Bill. All rights reserved.
//
//只要需要定位功能。。。在plist中添加。。。
//NSLocationWhenInUseUsageDescription->YES(當使用時)
//NSLocationAlwaysUsageDescription->YES(一直)
#import "ViewController.h"
//2.引入頭文件
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>//地圖庫
#import "TCAnnotation.h"
@interface ViewController ()<CLLocationManagerDelegate,
???????????????????????????? MKMapViewDelegate>{
??? //3.創建定位管理
??? CLLocationManager *_locationManager;
??? //定義地圖視圖
??? MKMapView *_mapView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
??? [super viewDidLoad];
?? ?
??? _mapView = [[MKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
??? _mapView.delegate = self;
??? [self.view addSubview:_mapView];
??? //設置用戶位置追蹤
??? _mapView.userTrackingMode = MKUserTrackingModeFollow;
//??? _mapView.mapType = MKMapTypeHybrid;
?? ?
??? //初始化管理器
??? _locationManager = [[CLLocationManager alloc]init];
??? //判斷定位服務是否開啟
//??? NSLog(@"%d",[CLLocationManager locationServicesEnabled]);
??? if (![CLLocationManager locationServicesEnabled]){
??????? NSLog(@"定位服務當前可能尚未打開,請設置打開");
??????? return;
??? }
??? //如果沒有授權,則請求用戶授權
??? if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)//表示授權狀態沒有決定,也就是沒有授權
??? {//如果授權沒有,請求授權
??????? [_locationManager requestWhenInUseAuthorization];
??? }
??? //如果授權狀態為whenInUse
??? else if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse){
??????? //設置管理器
??????? //設置代理
??????? _locationManager.delegate = self;
??????? //設置定位精度
??????? _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
??????? //定位頻率
??????? _locationManager.distanceFilter = 100;
??????? //啟動定位
??????? [_locationManager startUpdatingLocation];
??? }
??? [self addAnnotation];
}
#pragma mark 添加大頭針
-(void)addAnnotation{
??? CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(30.7266819435, 120.7208981251);
??? TCAnnotation *annonation1 = [[TCAnnotation alloc]init];
??? annonation1.title = @"智慧產業創新園";
??? annonation1.subtitle = @"東臣信息科技";
??? annonation1.coordinate = location1;
??? [_mapView addAnnotation :annonation1];
}
#pragma mark CoreLocation代理
#pragma mark 跟蹤定位代理方法,每次位置發生變化就會執行
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
??? CLLocation *location = [locations firstObject];//取出位置信息
??? CLLocationCoordinate2D coordinate = location.coordinate;//獲取位置坐標
?? ?
??? NSLog(@"經度為:%f",coordinate.longitude);
??? NSLog(@"緯度為:%f",coordinate.latitude);
??? NSLog(@"海拔:%f,航向:%f,行走速度:%f",location.altitude,location.course,location.speed);
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
??? //將地圖中心位置移動到某點
??? //_mapView
//??????? _mapView setCenterCoordinate:<#(CLLocationCoordinate2D)#>
??? //地圖顯示區域
??? MKCoordinateRegion theRegion;
??? theRegion.center = userLocation.coordinate;
??? theRegion.span.latitudeDelta = 0.02;
??? theRegion.span.longitudeDelta = 0.02;
??? [_mapView setRegion:theRegion];
}
- (void)didReceiveMemoryWarning {
??? [super didReceiveMemoryWarning];
??? // Dispose of any resources that can be recreated.
}
@end
//
//? TCAnnotation.h
//? Core Location_定位功能
//
//? Created by DC020 on 15/12/23.
//? Copyright (c) 2015年 Bill. All rights reserved.
//
#import <Foundation/Foundation.h>
//1.引入頭文件,遵守協議MKAnnotation
#import <MapKit/MapKit.h>
@interface TCAnnotation : NSObject<MKAnnotation>
@property(nonatomic,assign)CLLocationCoordinate2D coordinate;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *subTitle;
@end
//
//? ViewController.m
//? 地理編碼以及反地理編碼
//
//? Created by DC020 on 15/12/23.
//? Copyright (c) 2015年 Bill. All rights reserved.
//
//搜索的所有結果都是在中國境內的,因為蘋果在中國的地圖服務商是高德地圖
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController (){
??? CLGeocoder *_geoCoder;//創建地理編碼對象
?? ?
}
@end
@implementation ViewController
- (void)viewDidLoad {
??? [super viewDidLoad];
??? _geoCoder =[[CLGeocoder alloc]init];
??? //地理編碼...傳入的信息是地名
??? [self getCoordinateByAddress:@"浙江"];
??? //反地理編碼...傳入的信息是坐標(經緯度)
//??? [self getAddressByLatitude:30 longitude:120];
}
#pragma mark 根據地名確定地理坐標
-(void)getCoordinateByAddress:(NSString *)address{
??? [_geoCoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
??????? //取得地標,地標中存儲了詳細的地址信息。注意:一個地名可以搜索出多個地址
??????? CLPlacemark *placemark = [placemarks firstObject];
??????? //剖析地標
??????? //位置
??????? CLLocation *location = placemark.location;
??????? CLLocationCoordinate2D coordinate = location.coordinate;
??????? NSLog(@"經度是%f",coordinate.latitude);
??????? //區域
??????? CLRegion *region = placemark.region;
??????? NSLog(@"%@",region);
??????? //詳細地址
??????? NSDictionary*addressDic = placemark.addressDictionary;
??????? NSLog(@"%@",addressDic);
?????? ?
??? }];
?? ?
}
#pragma mark 根據坐標取得地名
-(void)getAddressByLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude{
??? //通過經緯度創建位置信息
??? CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
?? ?
??? [_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
??????? //獲取地標
??????? CLPlacemark *placemark =[placemarks firstObject];
??????? //打印地標中的地址信息
??????? NSLog(@"%@",placemark.addressDictionary[@"FormattedAddressLines"]);
??? }];
}
- (void)didReceiveMemoryWarning {
??? [super didReceiveMemoryWarning];
??? // Dispose of any resources that can be recreated.
}
@end
轉載于:https://my.oschina.net/u/2501648/blog/550085
總結
- 上一篇: 软件盘控制的问题
- 下一篇: zabbix自动发现监控磁盘(iops和