objective-c 定位導航

ViewController.m 文件git



#import "ViewController.h"ide

//引入核心定位編碼

#import <CoreLocation/CoreLocation.h>atom

//地圖spa

#import <MapKit/MapKit.h>.net

//引入大頭針3d

#import "Annotation.h"代理

//NSLocationWhenInUseUsageDescription-->YES(當使用)code


//NSLocationAlwaysUseUsageDescription-->YES(一直)orm

@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>

{

    //建立管理者

    CLLocationManager * locationManager;

    //定義地圖

    MKMapView * mapViewWO;

    UITextView * textview;

    //建立地理編碼對象

    CLGeocoder * geoCoder;

    

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self layout];

    geoCoder=[CLGeocoder new];

    //初始化管理器

    locationManager=[CLLocationManager new];

    if (![CLLocationManager locationServicesEnabled]) {

        NSLog(@"定位服務當前可能還沒有打開,請設置打開");

        return ;

    }

    //若是沒有受權,則請求用戶受權

    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined) {

        //若是沒 請求受權

        [locationManager requestWhenInUseAuthorization];

    }else if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){

        //遵照代理

        locationManager.delegate=self;

        //設置定位精度

        locationManager.desiredAccuracy=kCLLocationAccuracyBest;

        //定位頻率

        locationManager.distanceFilter=100;

        //啓動設置

        [locationManager startUpdatingLocation];

        

    }

}

#pragma mark CoreLocation 代理

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

    NSLog(@"dfsf");

    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)layout{

    textview=[[UITextView alloc]initWithFrame:CGRectMake(10, 20, 280, 30)];

    textview.layer.borderWidth=1;

    

    textview.layer.borderColor=[UIColor grayColor].CGColor;

    [self.view addSubview:textview];

    

    UIButton * souSuo=[[UIButton alloc]initWithFrame:CGRectMake(300, 20, 60, 30)];

    [souSuo setTitle:@"搜索" forState:UIControlStateNormal];

    [souSuo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [souSuo addTarget:self action:@selector(sousuo) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:souSuo];

    

    mapViewWO=[[MKMapView alloc]initWithFrame:CGRectMake(0, 50, 375, 667)];

    [self.view addSubview:mapViewWO];

    mapViewWO.delegate=self;

    //設置用戶位置追蹤

    mapViewWO.userTrackingMode=MKUserTrackingModeFollow;

   

    

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    NSLog(@"s");

    //將地圖中心位置移動到某點

    //mapView setCenterCoordinate:(CLLocationCoordinate2D)

    //地圖顯示區域

    MKCoordinateRegion theRegion;

    theRegion.center=userLocation.coordinate;

    theRegion.span.latitudeDelta=0.1;

    theRegion.span.longitudeDelta=0.1;

    [mapViewWO setRegion:theRegion];

   }

-(void)getDiMing:(NSString*)diming{

    

    [geoCoder geocodeAddressString:diming completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //取得地標,地標中儲存了詳細的地址信息,

        CLPlacemark *placemark=[placemarks firstObject ];

        NSLog(@"%@",placemark);

        //剖析地標

        //位置

        CLLocation * location=placemark.location;

        CLLocationCoordinate2D zuobiao=location.coordinate;

        NSLog(@"%@",location);

        //區域

        CLRegion *region=placemark.region;

        NSLog(@"%@",region);

        //詳細地址

        NSDictionary * dic=placemark.addressDictionary;

        NSLog(@"%@",dic);

        NSLog(@"%@",dic[@"Country"]);//國家

        NSLog(@"%@",dic[@"Name"]);//

        NSLog(@"%@",dic[@"State"]);

        NSLog(@"%@",dic[@"FormattedAddressLines"][0]);//詳細地址

        

        

        //大頭針位置

        CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(zuobiao.latitude, zuobiao.longitude);

        Annotation * annotation=[Annotation new];

        annotation.title=dic[@"Name"];

        annotation.subtitle=dic[@"FormattedAddressLines"][0];

        annotation.coordinate=location1;

        [mapViewWO addAnnotation:annotation];


        

        //將地圖中心位置移動到某點

        //mapView setCenterCoordinate:(CLLocationCoordinate2D)

        //地圖顯示區域

        MKCoordinateRegion theRegion;

        theRegion.center=location1;

        theRegion.span.latitudeDelta=0.1;

        theRegion.span.longitudeDelta=0.1;

        [mapViewWO setRegion:theRegion];

    }];

    

}


-(void)sousuo{

    NSLog(@"sdfsdfsdf");

    [self getDiMing:textview.text];

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//

//  Annotation.h

//  Core Location 定位功能

//

//  Created by DC017 on 15/12/23.

//  Copyright © 2015 DC017. All rights reserved.

//

@end

_______________________________________________________________

大頭針.h文件

#import <Foundation/Foundation.h>

//引入頭文件,遵照協議---MKAnnotation

#import <MapKit/MapKit.h>


@interface Annotation : NSObject<MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@property(nonatomic,copy)NSString * title;

@property(nonatomic,copy)NSString * subtitle;

@end

相關文章
相關標籤/搜索