使用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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值