天天看點

使用MapKit API現實使用者目前位置 放置标注

注意:添加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