//
// 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
记录一下今天的地图代码,火星坐标和地球坐标的问题
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 成长记录-连载(三十六) ——我的第一篇五千字长文,说了什么,你一定想不到 并不是不想每天写公众号,而是之前思考怎...