#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) CLLocationManager *mgr;
@end
@implementation ViewController
- (IBAction)addAnnotations:(id)sender {
MKPointAnnotation *annotation = [MKPointAnnotation new];
CLLocationDegrees latitude = 35.4332 + arc4random_uniform(10);
CLLocationDegrees longitude = 119.3342 + arc4random_uniform(10);
[annotation setCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
annotation.title = @"China";
annotation.subtitle = @"City";
[self.mapView addAnnotation:annotation];
MKCoordinateSpan span = MKCoordinateSpanMake(21, 22);
MKCoordinateRegion region = MKCoordinateRegionMake(annotation.coordinate, span);
[self.mapView setRegion:region];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
NSLog(@"%s",__func__);
static NSString *identifier = @"annotation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(!annotation) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = YES;
annotationView.pinTintColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.4 alpha:1.0];
annotationView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeCustom];
} else {
annotationView.annotation = annotation;
}
return annotationView;
}
- (void)viewDidLoad {
[super viewDidLoad];
//1.初始化manager对象
self.mgr = [CLLocationManager new];
//2.征求用户的同意/授权(>=iOS8.0+假设用户同意)+Info.plist添加key
[self.mgr requestWhenInUseAuthorization];
//3.设置mapView属性(和定位相关属性)
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
//4.设置mapView的代理
self.mapView.delegate = self;
self.mapView.rotateEnabled = NO;
self.mapView.mapType = MKMapTypeStandard;
}
//监听用户的位置
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
//打印用户的位置
NSLog(@"纬度%f; 经度%f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
userLocation.title = @"userLocation";
userLocation.subtitle = @"detailLocation";
}
//监听用户挪动地图停止的时机
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//region:设备上显示的那部分称为地图视图的区域
NSLog(@"用户挪动地图视图停止...");
}
@end
iOS中MKMapView的使用
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近,《火星情报局》骤然火起,除了汪涵出走芒果台的新闻之外,还有就是因为网红薛之谦。作为一名“过气老艺人”,薛之谦...