//
// ViewController.m
// MapViewIntroduction
//
// Created by hq on 16/5/17.
// Copyright 2016年 hanqing. All rights reserved.
//
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <MKMapViewDelegate,CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property(nonatomic,strong) CLLocationManager *clm;
@end
@implementation ViewController
-(CLLocationManager *)clm{
if (_clm==nil) {
_clm=[[CLLocationManager alloc]init];
if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0) {
[_clm requestAlwaysAuthorization];
}
}
return _clm;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate=self;
self.mapView.mapType=MKMapTypeHybrid;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self clm];
self.mapView.rotateEnabled=NO;
//显示用户位置,需要请求权限
self.mapView.showsUserLocation=YES;
//该方法会自动放到到用户所在到区域,不用我们再缩放
//self.mapView.userTrackingMode=MKUserTrackingModeFollowWithHeading;
}
//用户的位置发生了变化就会调用该方法
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocation *loc=userLocation.location;
//将经纬度转换为我们的地点
CLGeocoder *geo=[[CLGeocoder alloc]init];
[geo reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//获取一个地标
CLPlacemark *mark=[placemarks firstObject];
//设置地理位置上边的标题
userLocation.title=mark.administrativeArea;
userLocation.subtitle=mark.locality;
}];
//设置地图显示中心为我们的坐标点,不能设置我们的放大范围
//[self.mapView setCenterCoordinate:userLocation.location.coordinate];
//设置中心点,同时设置放大的范围
//后边的两个数不知道填啥,可以通过我们的代理方法regionDidChangeAnimated获取合适的值
//使用该方法,就不能使用自动缩放到用户所在到位置了,只能通过self.mapView.showsUserLocation=YES;
//该方法会自动放到到用户所在到区域,不用我们再缩放
//self.mapView.userTrackingMode=MKUserTrackingModeFollowWithHeading;
MKCoordinateSpan span=MKCoordinateSpanMake(0.005361, 0.004050);
MKCoordinateRegion region=MKCoordinateRegionMake(userLocation.location.coordinate, span);
[self.mapView setRegion:region animated:YES];
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
MapKit显示用户的位置
最新推荐文章于 2024-09-10 14:09:08 发布
714

被折叠的 条评论
为什么被折叠?



