iOS_MKMapView的使用

來源:互聯網
上載者:User

標籤:

#import "ViewController.h"#import <MapKit/MapKit.h>#import <CoreLocation/CoreLocation.h>@interface ViewController ()<MKMapViewDelegate>@property(nonatomic,strong) CLLocationManager *locMgr;@property (weak, nonatomic) IBOutlet MKMapView *mapView;@end@implementation ViewController-(CLLocationManager *)locMgr{    if (_locMgr==nil) {        _locMgr=[[CLLocationManager alloc]init];    }    return _locMgr;}- (void)viewDidLoad {    [super viewDidLoad];    if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {        [self.locMgr requestWhenInUseAuthorization];    }        //顯示使用者位置(藍色發光圓圈),還有None和FollowWithHeading兩種,當有這個屬性的時候,iOS8第一次開啟地圖,會自動定位並顯示這個位置。iOS7模擬器上不會。    self.mapView.userTrackingMode=MKUserTrackingModeFollow;        //地圖模式,預設是standard模式,還有衛星模式satellite和雜交模式Hybrid    self.mapView.mapType=MKMapTypeStandard;        //設定代理    self.mapView.delegate=self;}//MKUserLocation是地圖上圖釘模型,有title和subtitle以及location資訊。該方法調用頻繁-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{    NSLog(@"%f,%f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);    //點擊圖釘,會出現以下資訊    [email protected]"中國";    [email protected]"四大文明古國之一";        //讓地圖顯示使用者的位置(iOS8一開啟地圖會預設轉到使用者所在位置的地圖),該方法不能設定地圖精度//    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];        //這個方法可以設定地圖精度以及顯示使用者所在位置的地圖    MKCoordinateSpan span=MKCoordinateSpanMake(0.1, 0.1);    MKCoordinateRegion region=MKCoordinateRegionMake(userLocation.location.coordinate, span);    [mapView setRegion:region animated:YES];}//可以列印經緯度的跨度,用來測試當前視圖下地經緯度跨度是多少,然後用於上面的MKCoordinateSpanMake方法中-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{    NSLog(@"%f,%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);}@end


(1)如果是iOS8模擬器,會出現如下報錯:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.


需要添加如下,還需要增加plist中添加NSLocationWhenInUseUsageDescription為YES:

 

if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) {        [self.locMgr requestWhenInUseAuthorization];    }


(2)代理方法

 

常用的就是使用者位置更新,地區改變時調用的代理方法。


(3)問題

??顯示使用者位置所在的地圖時,使用者位置有時候不在正中間,而是在偏右的地方。難道是模擬器的BUG?


(4)添加一個返回到當前位置的按鈕,添加方法:

 

- (IBAction)backToUserLocation:(id)sender {    [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES];}


(5)添加圖釘

 

一般順序是:建立圖釘對象 >>> 設定圖釘的座標,標題和子標題 >>> 最後把圖釘add添加到地圖中

核心是:系統內建的圖釘不能設定座標(唯讀),所以我們一般是自己寫一個繼承NSObject的模型,把coordinate,title,subtile屬性寫進去,關鍵是這個模型需要遵循<MKAnnotation>。然後用這個圖釘類去建立圖釘對象。下面就是自訂的圖釘類。

 

#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface WPAnnotation : NSObject<MKAnnotation>@property (nonatomic, assign) CLLocationCoordinate2D coordinate;@property (nonatomic, copy) NSString *title;@property (nonatomic, copy) NSString *subtitle;@end


然後使用:

 

 

- (void)viewDidLoad {    [super viewDidLoad];    ......    //監聽點擊,圖釘    [self.mapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMapView:)]];}-(void)tapMapView:(UITapGestureRecognizer *)tap{    //擷取點擊的點    CGPoint point=[tap locationInView:tap.view];    //點轉換成座標    CLLocationCoordinate2D coordi=[self.mapView convertPoint:point toCoordinateFromView:self.mapView];    //建立圖釘模型,設定圖釘的座標,然後把圖釘加進去    WPAnnotation *anno=[[WPAnnotation alloc]init];    anno.coordinate=coordi;    [email protected]"拙政園";    [email protected]"蘇州市博物館附近";    [self.mapView addAnnotation:anno];}
本文轉自:http://www.itnose.net/detail/6200956.html

iOS_MKMapView的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.