MapView的簡單實用

在storyBoard中使用到非UIKit的框架,要在項目的TARGETS-->General-->Linked Frameworks and Libraries中點擊+號添加MapKit框架,遵守MKMapViewDelegate協議

@interface ViewController ()<MKMapViewDelegate>
    @property (weak, nonatomic) IBOutlet MKMapView *mapView;
    @property (nonatomic, strong) CLLocationManager *locationManager;
@end
// 1.請求用戶授權
    // 創建對象并請求授權
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];
    
    /*
     MKMapTypeStandard     標準地圖
     MKMapTypeSatellite    衛星地圖
     MKMapTypeHybrid       鳥瞰地圖
     MKMapTypeSatelliteFlyover
     MKMapTypeHybridFlyover
     */
    // 設置地圖類型
    self.mapView.mapType = MKMapTypeStandard;
    // 實時的交通情況 -->iOS9新增方法
    self.mapView.showsTraffic = YES;
    
    // 2.用戶跟蹤模式
    /*
     MKUserTrackingModeNone  無
     MKUserTrackingModeFollow 跟蹤位置
     MKUserTrackingModeFollowWithHeading 跟蹤位置并跟蹤方向
     */
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    // 3.設置代理
    self.mapView.delegate = self;

確定當前位置(region-->MKCoordinateRegion)
確定一個區域和確定一個圓類似,需要且只需要確定兩個元素:1.中心點經緯度CLLocationCoordinate2D-->self.mapView.userLocation.coordinate 2.經緯度跨度span-->MKCoordinateSpan 兩個都是結構體,直接make。
如何確定系統默認經緯度跨度 --> 在mapView的代理方法regionDidChangeAnimated中,當地圖區域改變時回自動調用,在此方法中打印經緯度跨度。
如果需要動畫回到當前位置,調用mapView的 setRegion: animated:
當區域改變的時候調用

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    NSLog(@"latitudeDelta:%f     longitudeDelta:%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);
}

方法

點擊按鈕回到初始位置

- (IBAction)backToCurrentLocation:(id)sender
{
//    self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
    // 區域 region --兩個結構體 center span
    // center 經度和緯度
    // span   經度跨度和緯度跨度
    // 當前區域的中心點經緯度
    CLLocationCoordinate2D center = self.mapView.userLocation.location.coordinate;
    // 當前區域的經緯度跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(0.021251, 0.016093);
    // 確定區域
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
//    self.mapView.region = region;
    // 加入系統動畫
    [self.mapView setRegion:region animated:YES];
}

放大和縮小地圖其實就是:"當前"地圖顯示的中心點經緯度不變,經緯度跨度(span)縮小或增大2倍;
注意:在回到當前位置設置region時的中心點經緯度是定位到用戶的經緯度(userLocation),而放大縮小地圖的中心點經緯度是當前地圖界面的中心點經緯度。

點擊按鈕放大地圖

// 放大地圖
- (IBAction)biggerMap:(id)sender
{
    // 緯度跨度
    CGFloat latitudeDelta = self.mapView.region.span.latitudeDelta * 0.5;
    // 經度跨度
    CGFloat longitudeDelta = self.mapView.region.span.longitudeDelta * 0.5;
    // 經緯度跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
    // 確定區域
    [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.centerCoordinate, span) animated:YES];
}

點擊按鈕縮小地圖

- (IBAction)smallerMap:(id)sender
{
    // 緯度跨度
    CGFloat latitudeDelta = self.mapView.region.span.latitudeDelta * 2;
    // 經度跨度
    CGFloat longitudeDelta = self.mapView.region.span.longitudeDelta * 2;
    // 經緯度跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
    // 確定區域
    [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.centerCoordinate, span) animated:YES];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容