注意:添加CoreLocation及MapKit框架;在Info.plist中添加NSLocationWhenInUseUsageDescription及提示信息
//
// ViewController.m
// MyAddressMap
//
// Created by MQL on 15/3/20.
// Copyright (c) 2015年 MQL. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
{
CLLocationManager *locationManager;
id <MKAnnotation> annotation;
}
@property (nonatomic, weak) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//应用启动时的默认操作
if (locationManager == nil) {
locationManager = [[CLLocationManager alloc]init];
}
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0)
{
//设置定位权限 仅ios8有意义
[locationManager requestWhenInUseAuthorization];// 前台定位
}
//应用启动时的默认操作
}
#pragma mark --MKMapViewDelegate
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//放大地图
userLocation.title = @"";
MKCoordinateRegion regin = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 600, 600);
[self.mapView setRegion:regin];
//添加大头针
if (annotation) {
[self.mapView removeAnnotation: annotation];
}
MKPointAnnotation *point = [[MKPointAnnotation alloc]init];
annotation = point;
point.coordinate = userLocation.coordinate;
point.title = @"北京市朝阳区广顺北大街33号院1号楼福码大厦B座12层";
[self.mapView addAnnotation:point];
}
@end