博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
地理位置查询
阅读量:6304 次
发布时间:2019-06-22

本文共 7755 字,大约阅读时间需要 25 分钟。

hot3.png

//

//  MapViewController.m

//  地理位置查询

//

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

//  Copyright © 2015 崔晓宇. All rights reserved.

//

#import "MapViewController.h"

#import <MapKit/MapKit.h>

#import <CoreLocation/CoreLocation.h>

#import "CXYAnnotation.h"

#import "ViewController.h"

MapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>

{

    MKMapView *_mapView;

    CLLocationManager *_locationManager;

    UIButton *_backButton;

    ViewController *_viewController;

}

@implementation MapViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    _mapView = [[MKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    _mapView.userTrackingMode = MKUserTrackingModeFollow;

    _mapView.delegate = self;

    [self.view addSubview:_mapView];

    

    _backButton = [[UIButton alloc]initWithFrame:CGRectMake(300, 20, 50, 20)];

    [_backButton setTitle:@"back" forState:UIControlStateNormal];

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

    [_backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    [_mapView addSubview:_backButton];

    

    

    //初始化管理器

    _locationManager = [[CLLocationManager alloc]init];

    //判断定位服务是否开启

    if ( ![CLLocationManager locationServicesEnabled]) {

        NSLog(@"定位服务当前可能尚未打开,请设置打开!");

        return;

    }

    else{

        NSLog(@"定位服务已经打开");

    }

    //如果没有授权,则请求用户授权

    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {

        //如果没,请求授权

        [_locationManager requestWhenInUseAuthorization];

    }

    //如果授权状态为whenInUse

    if([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusAuthorizedWhenInUse){

        //设置管理器

        //设置管理器代理--CLLocationManagerDelegate

        _locationManager.delegate = self;

        //设置定位精度

        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //设置频率

        _locationManager.distanceFilter = 100;

        //启动定位

        [_locationManager startUpdatingLocation];

    }

    

    [self addAnnotation];

    _viewController = [[ViewController alloc]init];

}

- (void)back{

    [self dismissViewControllerAnimated:_viewController completion:nil];

}

#pragma mark 添加大头针

- (void)addAnnotation{

    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(30.7266819435, 120.7208981251);

    CXYAnnotation *annonation1 = [[CXYAnnotation alloc]init];

    annonation1.title = @"智慧创业创新园";

    annonation1.subtitle = @"东臣信息科技";

    annonation1.coordinate = location1;

    [_mapView addAnnotation:annonation1];

    

}

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

    NSLog(@"1");

    //将地图中心位置移动到某点

    //    _mapView setCenterCoordinate:<#(CLLocationCoordinate2D)#>

    //地图显示区域

    MKCoordinateRegion theRegion;

    theRegion.center = userLocation.coordinate;

    theRegion.span.latitudeDelta = 0.1;

    theRegion.span.longitudeDelta = 0.1;

    [_mapView setRegion:theRegion];

}

#pragma mark CoreLocation代理

#pragma mark 跟踪定位的代理方法,每次位置发生变化就会执行

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)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)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end

//

//  ViewController.m

//  地理位置查询

//

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

//  Copyright © 2015 崔晓宇. All rights reserved.

//

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

#import "MapViewController.h"

@interface ViewController ()

{

    CLGeocoder *_geoCoder;

    MapViewController *_mapView;

    

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    _geoCoder = [[CLGeocoder alloc]init];

    [_button1 addTarget:self action:@selector(selector1) forControlEvents:UIControlEventTouchUpInside];

    _adress_Label.numberOfLines = 0;

    _adress_fanLabel.numberOfLines = 0;

    [_button2 addTarget:self action:@selector(selector2) forControlEvents:UIControlEventTouchUpInside];

    _mapView = [[MapViewController alloc]init];

}

- (void)selector1{

    [self getCoordinateByAddress:_diMing_Text.text];

    [self presentViewController:_mapView animated:YES completion:nil];

    

}

- (void)selector2{

    [self getAddressByLatitude:[_jingDu_fanText.text floatValue] longitude:[_weiDu_fanText.text floatValue]];

   

}

#pragma mark 根据地名确定地理坐标

- (void)getCoordinateByAddress : (NSString *)address {

    

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

        

        CLPlacemark *placemark = [placemarks firstObject];

        CLLocation * location = placemark.location;

        CLLocationCoordinate2D coordinate = location.coordinate;

        NSLog(@"sadfas%f     dsfasfds%f",location.coordinate.longitude,location.coordinate.latitude);

        _jingDu_Label.text = [NSString stringWithFormat:@"%f",coordinate.latitude] ;

        NSLog(@"%f",coordinate.latitude);

        _weiDu_Label.text = [NSString stringWithFormat:@"%f",coordinate.longitude];

        

        

  

        NSDictionary *addressDic = placemark.addressDictionary;

        _adress_Label.text = addressDic[@"Name"];

        

    }];

}

#pragma mark 根据坐标取得地名

- (void)getAddressByLatitude : (CLLocationDegrees)latitude longitude : (CLLocationDegrees)longitude{

    //通过经纬度创建位置信息

    CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];

    [_geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        //获取地标

        CLPlacemark *placemark = [placemarks firstObject];

        NSLog(@"%@",placemark.addressDictionary[@"Name"]);

        _adress_fanLabel.text = placemark.addressDictionary[@"Name"];

    }];

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

//

//  ViewController.h

//  地理位置查询

//

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

//  Copyright © 2015 崔晓宇. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *diLiBianMa;

@property (weak, nonatomic) IBOutlet UILabel *diMing_diLIBianMa;

@property (weak, nonatomic) IBOutlet UILabel *jingDu_diLiBianMa;

@property (weak, nonatomic) IBOutlet UILabel *address_diLiBianMa;

@property (weak, nonatomic) IBOutlet UITextField *diMing_Text;

@property (weak, nonatomic) IBOutlet UILabel *jingDu_Label;

@property (weak, nonatomic) IBOutlet UILabel *weiDu_Label;

@property (weak, nonatomic) IBOutlet UILabel *adress_Label;

@property (weak, nonatomic) IBOutlet UILabel *jingDu_fan;

@property (weak, nonatomic) IBOutlet UILabel *weiDu_fan;

@property (weak, nonatomic) IBOutlet UITextField *jingDu_fanText;

@property (weak, nonatomic) IBOutlet UITextField *weiDu_fanText;

@property (weak, nonatomic) IBOutlet UILabel *adress_fanLabel;

@property (weak, nonatomic) IBOutlet UILabel *diMing_fanLabel;

@property (weak, nonatomic) IBOutlet UIButton *button1;

@property (weak, nonatomic) IBOutlet UIButton *button2;

@end

//

//  CXYAnnotation.h

//  地理位置查询

//

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

//  Copyright © 2015 崔晓宇. All rights reserved.

//

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface CXYAnnotation : NSObject<MKAnnotation>

@property(nonatomic,assign)CLLocationCoordinate2D coordinate;

@property(nonatomic,copy)NSString *title;

@property(nonatomic,copy)NSString *subtitle;

@end

//

//  CXYAnnotation.m

//  地理位置查询

//

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

//  Copyright © 2015 崔晓宇. All rights reserved.

//

#import "CXYAnnotation.h"

@implementation CXYAnnotation

@end

转载于:https://my.oschina.net/u/2499773/blog/552472

你可能感兴趣的文章
如何学习虚拟现实技术vr? vr初级入门教程开始
查看>>
第4 章序列的应用
查看>>
Mysql explain
查看>>
初识闭包
查看>>
java tcp socket实例
查看>>
011 指针的算术运算
查看>>
hdu1874畅通工程续
查看>>
rails 字符串 转化为 html
查看>>
java-学习8
查看>>
AOP动态代理
查看>>
Oracle序列
查看>>
xcodebuild命令行编译错误问题解决
查看>>
Yii2.0 下的 load() 方法的使用
查看>>
华为畅玩5 (CUN-AL00) 刷入第三方twrp Recovery 及 root
查看>>
LeetCode----67. Add Binary(java)
查看>>
母版页 MasterPage
查看>>
[转] ReactNative Animated动画详解
查看>>
DNS原理及其解析过程
查看>>
记录自写AFNetWorking封装类
查看>>
没想到cnblog也有月经贴,其实C#值不值钱不重要。
查看>>