一、需求

效果圖.png
1.設(shè)計(jì): 將用戶當(dāng)前位置,顯示在地圖中間,而且點(diǎn)擊藍(lán)色點(diǎn),彈出大頭針
-
2.實(shí)現(xiàn)步驟:
- 查看當(dāng)前?用戶位置信息
- 設(shè)置地圖代理,并實(shí)現(xiàn)代理?方法,在代理?方法中獲取?用戶當(dāng)前位置(注意:iOS8.0之后要請求授權(quán)!!)
- 將地圖顯?示中?心調(diào)整為?用戶當(dāng)前所在位置(iOS8.0之前,地圖不會(huì)?自動(dòng)移動(dòng)到?用戶所在位置)
- 調(diào)整當(dāng)前地圖顯?示的區(qū)域(可使?用對應(yīng)代理?方法查看當(dāng)前地圖跨度,然后調(diào)整到合適跨度即可)
- 查看當(dāng)前?用戶位置信息
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/**
MKUserLocation : 被稱作“大頭針模型”,其實(shí)喊什么都行,本質(zhì)就是一個(gè)數(shù)據(jù)模型,只不過此模型遵循了大頭針要遵循的協(xié)議(MKAnnotation)
location: 用戶當(dāng)前所在位置信息(CLLocation對象)
title: 大頭針標(biāo)注要顯示的標(biāo)題(NSString對象)
subtitle: 大頭針標(biāo)注要顯示的子標(biāo)題(NSString對象)
*/
// 根據(jù)用戶當(dāng)前位置的經(jīng)緯度,設(shè)置地圖顯示中心
/**
存在弊端:地圖顯示比例過大,無法調(diào)整
解決方案:直接使用對應(yīng)的調(diào)整地圖“顯示區(qū)域”的API
*/
// [mapView setCenterCoordinate:userLocation.coordinate animated:YES];
/**
MKCoordinateSpan 跨度解釋:
latitudeDelta:緯度跨度,因?yàn)槟媳本暩?0度,所以此值的范圍是(0---180);此值表示,整個(gè)地圖視圖寬度,顯示多大跨度
longitudeDelta:經(jīng)度跨度,因?yàn)闁|西經(jīng)各180度,所以此值范圍是(0---360):此值表示,整個(gè)地圖視圖高度,顯示多大跨度
注意:地圖視圖顯示,不會(huì)更改地圖的比例,會(huì)以地圖視圖高度或?qū)挾容^小的那個(gè)為基準(zhǔn),按比例調(diào)整
*/
// MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
// MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, span);
// [mapView setRegion:region animated:YES];
}
// 當(dāng)?shù)貓D區(qū)域(跨度)改變時(shí)調(diào)用
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"%f---%f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
}
二、完整實(shí)例:
- 代碼實(shí)現(xiàn):
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
/** 位置管理者 */
@property (nonatomic, strong) CLLocationManager *locationM;
@end
@implementation ViewController
#pragma mark -懶加載
-(CLLocationManager *)locationM
{
if (!_locationM) {
_locationM = [[CLLocationManager alloc] init];
// 版本適配
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
[_locationM requestAlwaysAuthorization];
}
}
return _locationM;
}
- (void)viewDidLoad {
[super viewDidLoad];
// MKMapTypeStandard = 0, // 標(biāo)準(zhǔn)地圖
// MKMapTypeSatellite, // 衛(wèi)星云圖
// MKMapTypeHybrid, // 混合(在衛(wèi)星云圖上加了標(biāo)準(zhǔn)地圖的覆蓋層)
// MKMapTypeSatelliteFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D立體
// MKMapTypeHybridFlyover NS_ENUM_AVAILABLE(10_11, 9_0), // 3D混合
// 設(shè)置地圖顯示樣式(必須注意,設(shè)置時(shí) 注意對應(yīng)的版本)
self.mapView.mapType = MKMapTypeStandard;
// 設(shè)置地圖的控制項(xiàng)
// 是否可以滾動(dòng)
// self.mapView.scrollEnabled = NO;
// 縮放
// self.mapView.zoomEnabled = NO;
// 旋轉(zhuǎn)
// self.mapView.rotateEnabled = NO;
// 設(shè)置地圖的顯示項(xiàng)(注意::版本適配)
// 顯示建筑物
self.mapView.showsBuildings = YES;
// 指南針
self.mapView.showsCompass = YES;
// 興趣點(diǎn)
self.mapView.showsPointsOfInterest = YES;
// 比例尺
self.mapView.showsScale = YES;
// 交通
self.mapView.showsTraffic = YES;
// 顯示用戶位置
[self locationM];
// 顯示用戶位置, 但是地圖并不會(huì)自動(dòng)放大到合適比例
self.mapView.showsUserLocation = YES;
/**
* MKUserTrackingModeNone = 0, 不追蹤
MKUserTrackingModeFollow, 追蹤
MKUserTrackingModeFollowWithHeading, 帶方向的追蹤
*/
// 不但顯示用戶位置, 而且還會(huì)自動(dòng)放大地圖到合適的比例(也要進(jìn)行定位授權(quán))
// 不靈光
// self.mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
}
#pragma mark -MKMapViewDelegate
/**
* 當(dāng)?shù)貓D獲取到用戶位置時(shí)調(diào)用
*
* @param mapView 地圖
* @param userLocation 大頭針數(shù)據(jù)模型
*/
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/**
* MKUserLocation : 專業(yè)術(shù)語: 大頭針模型 其實(shí)喊什么都行, 只不過這個(gè)類遵循了大頭針數(shù)據(jù)模型必須遵循的一個(gè)協(xié)議 MKAnnotation
// title : 標(biāo)注的標(biāo)題
// subtitle : 標(biāo)注的子標(biāo)題
*/
userLocation.title = @"標(biāo)題";
userLocation.subtitle = @"子標(biāo)題";
// 移動(dòng)地圖的中心,顯示用戶的當(dāng)前位置
// [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
// 顯示地圖的顯示區(qū)域
// 控制區(qū)域中心
CLLocationCoordinate2D center = userLocation.location.coordinate;
// 設(shè)置區(qū)域跨度
MKCoordinateSpan span = MKCoordinateSpanMake(0.077919, 0.044529);
// 創(chuàng)建一個(gè)區(qū)域
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
// 設(shè)置地圖顯示區(qū)域
[mapView setRegion:region animated:YES];
}
/**
* 地圖區(qū)域已經(jīng)改變時(shí)帶哦用
*
* @param mapView 地圖
* @param animated 動(dòng)畫
*/
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"%f---%f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
}
/**
* 地圖區(qū)域?qū)⒁淖儠r(shí)帶哦用
*
* @param mapView 地圖
* @param animated 動(dòng)畫
*/
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
}
@end