美文网首页
记录一下今天的地图代码,火星坐标和地球坐标的问题

记录一下今天的地图代码,火星坐标和地球坐标的问题

作者: Jinkuro | 来源:发表于2016-12-08 23:28 被阅读37次
    //
    //  ViewController.m
    //  mapLearning
    //
    //  Created by Jin on 2016/12/6.
    //  Copyright © 2016年 Jin. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <MapKit/MapKit.h>
    #import <CoreLocation/CoreLocation.h>
    #import "JMLocationTransformer.h"
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    
    @interface ViewController () <MKMapViewDelegate>
    
    @property (nonatomic, strong) MKMapView *mapView;
    @property (nonatomic, strong) CLLocationManager *manager;
    @property (nonatomic, strong) UILabel *placeMarkerLabel;
    @property (nonatomic, strong) UILabel *lonAndLatLabel;
    
    @end
    
    @implementation ViewController
    
    - (UILabel *)lonAndLatLabel {
        if (!_lonAndLatLabel) {
            _lonAndLatLabel = [[UILabel alloc]initWithFrame:CGRectMake(8,SCREEN_HEIGHT - 76, SCREEN_WIDTH - 16, 30)];
            _lonAndLatLabel.backgroundColor = [UIColor whiteColor];
        }
        return _lonAndLatLabel;
    }
    
    - (UILabel *)placeMarkerLabel {
        if (!_placeMarkerLabel) {
            _placeMarkerLabel = [[UILabel alloc]initWithFrame:CGRectMake(8,SCREEN_HEIGHT - 38, SCREEN_WIDTH - 16, 30)];
            _placeMarkerLabel.backgroundColor = [UIColor whiteColor];
        }
        return _placeMarkerLabel;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
        view.center = self.view.center;
        view.backgroundColor = [UIColor redColor];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 5.0f;
        
        self.mapView = [[MKMapView alloc]initWithFrame:self.view.bounds];
        self.mapView.delegate = self;
        [self.view addSubview:self.mapView];
        [self.mapView addSubview:self.lonAndLatLabel];
        [self.mapView addSubview:self.placeMarkerLabel];
        
        [self.view addSubview:view];
    
        self.manager = [[CLLocationManager alloc]init];
        [self.manager requestAlwaysAuthorization];
        [self.manager requestWhenInUseAuthorization];
        
        self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    //    CGPoint point = [self.mapView convertCoordinate:self.mapView.centerCoordinate toPointToView:self.mapView];
    //    NSLog(@"Point = (%f,%f)",point.x,point.y);
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
        CLLocationCoordinate2D centerCoordinate = mapView.region.center;
        
        CLLocation *centerLocation = [[CLLocation alloc]initWithLatitude:centerCoordinate.latitude longitude:centerCoordinate.longitude];
    //    self.lonAndLatLabel.text = [NSString stringWithFormat:@"longitude = %f,latitude = %f",centerCoordinate.longitude,centerCoordinate.latitude];
        
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        // 反地理编码;根据经纬度查找地名
        [geocoder reverseGeocodeLocation:centerLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks.count == 0 || error) {
                NSLog(@"error");
                return;
            }
            CLPlacemark *pm = [placemarks firstObject];
            self.lonAndLatLabel.text = [NSString stringWithFormat:@"原生地标%@",pm.name];
        }];
        
        CLLocationCoordinate2D transformCoordinate = [JMLocationTransformer transformFromWGSToGCJ:centerCoordinate];
        CLLocation *transformLocation = [[CLLocation alloc]initWithLatitude:transformCoordinate.latitude longitude:transformCoordinate.longitude];
        
        CLGeocoder *geocoder1 = [[CLGeocoder alloc] init];
        // 反地理编码;根据经纬度查找地名
        [geocoder1 reverseGeocodeLocation:transformLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks.count == 0 || error) {
                NSLog(@"error");
                return;
            }
            CLPlacemark *pm = [placemarks firstObject];
            self.placeMarkerLabel.text = [NSString stringWithFormat:@"转换地标%@",pm.name];
        }];
        
    //    self.placeMarkerLabel.text = [NSString stringWithFormat:@"longitude = %f,latitude = %f",transformCoordinate.longitude,transformCoordinate.latitude];
    }
    
    /*
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        
        // 反地理编码;根据经纬度查找地名
        [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks.count == 0 || error) {
                NSLog(@"找不到该位置");
                return;
            }
            
            // 当前地标
            CLPlacemark *pm = [placemarks firstObject];
            
            // 区域名称
            userLocation.title = pm.locality;
            // 详细名称
            userLocation.subtitle = pm.name;
        }];
    }
    */
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:记录一下今天的地图代码,火星坐标和地球坐标的问题

          本文链接:https://www.haomeiwen.com/subject/ipfemttx.html