iOS定位服務與地圖開發(4)---顯示地圖

來源:互聯網
上載者:User

標籤:des   style   blog   class   code   java   

iOS 6之前,蘋果使用的是Google地圖,iOS 6之後,蘋果使用了自己的地圖(國內好像用的是高德地圖),但是API編程介面與iOS 5相比沒有太大變化。

iOS 應用程式中使用Map Kit API開發地圖應用程式,使用MKMapView類作為地圖顯示視圖,其委託協議是MKMapViewDelegate。

1、顯示地圖:

.h檔案中代碼

#import <MapKit/MapKit.h>@interface YXCViewController <MKMapViewDelegate>@end

.m檔案類實現代碼

@implementation YXCViewController- (void)viewDidLoad{    [super viewDidLoad];    // 設定地圖的類型    self.mapView.mapType = MKMapTypeStandard;    // 設定代理    self.mapView.delegate = self;}#pragma  mark - MKMapViewDelegate代理方法// 失敗回調- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error{    NSLog(@"error : %@",error.localizedDescription);}@end

地圖類型有3種:

MKMapTypeStandard:標準地圖。

MKMapTypeSatellite:衛星地圖類型。

MKMapTypeHybrid:混合地圖類型。

2、添加標註:

如果要在地圖視圖上添加標註點,需要2個步驟:1)觸發添加動作;2)實現地圖委託方法mapView:viewForAnnotation:,完成添加標註。

1)觸發添加動作:

- (IBAction)geocodeQuery:(id)sender {        // 從介面文字框擷取查詢地址字串    if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {        return ;    }        CLGeocoder *geocoder = [[CLGeocoder alloc] init];        [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {                NSLog(@"查詢記錄數:%i",[placemarks count]);                if ([placemarks count] > 0) {            // 移除目前地圖上所有標註點【否則單擊查詢按鈕,你會發現地圖上的標註點越來越多】            [self.mapView removeAnnotations:_mapView.annotations];        }                for (int i = 0; i < [placemarks count]; i++) {                        CLPlacemark *placemark = placemarks[i];            // 關閉鍵盤            [_txtQueryKey resignFirstResponder];                        // 調整地圖位置和縮放比例            MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 1000, 1000);            [_mapView setRegion:viewRegion animated:YES];                        // 執行個體化自訂的實現了MKAnnotation協議的地表徵圖注點類            MapLocation *annoation = [[MapLocation alloc] init];            annoation.streetAddress = placemark.thoroughfare;            annoation.city = placemark.locality;            annoation.state = placemark.administrativeArea;            annoation.zip = placemark.postalCode;            annoation.coordinate = placemark.location.coordinate;                        // 把標註點對象添加到地圖上            // 該方法一定會調用代理方法mapView: viewForAnnotation:            [_mapView addAnnotation:annoation];        }    }];}

MKCoordinateRegionMakeWithDistance()函數封裝了一個表示地圖地區的結構體:

typedef struct {

    CLLocationCoordinate2D center;   // 中心點

    MKCoordinateSpan span;  // 跨度

}MKCoordinateRegion ;

2)實現代理方法,完成添加標註

// 委託方法,在地圖視圖添加標註的時候回調- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{    // 獲得地表徵圖注對象    MKPinAnnotationView *annoationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOATION"];    if (annoationView == nil) {        annoationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PIN_ANNOATION"];    }    // 設定圖釘標註視圖的顏色為紫色    annoationView.pinColor= MKPinAnnotationColorPurple;    // 設定標註視圖時候是否動畫顯示在地圖上    annoationView.animatesDrop = YES;    // 在標註上可以顯示一些附加資訊,為YES情況下單擊“圖釘”時會出現一個氣泡    // 氣泡中文字資訊封裝在MapLocation對象中    annoationView.canShowCallout = YES;        return annoationView;}

自訂標註類MapLocation實現:

首先需要引入<Mapkit/MapKit.h>標頭檔,因為MKAnnotation協議是包含在該架構中。

- (NSString *)title :標註點上的主標題

- (NSString *)subTitle:標註點上副標題

.h標頭檔中代碼:

#import <MapKit/MapKit.h>@interface MapLocation : NSObject<MKAnnotation>// 地理座標@property (nonatomic , readwrite) CLLocationCoordinate2D coordinate;// 街道資訊屬性@property (nonatomic , copy) NSString *streetAddress;// 城市資訊屬性@property (nonatomic , copy) NSString *city;// 州、省、市資訊@property (nonatomic , copy) NSString *state;// 郵編@property (nonatomic , copy) NSString *zip;@end

.m中代碼:

 

@implementation MapLocation- (NSString *)title{    return @"您的位置!";}- (NSString *)subtitle{    NSMutableString *ret = [[NSMutableString alloc] init];    if (_state)        [ ret appendString:_state];    if (_city) {        [ret appendString:_city];    }        if (_city && _state) {        [ret appendString:@","];    }    if (_streetAddress && (_city || _state || _zip)) {        [ret appendString:@"?"];    }    if (_streetAddress) {        [ret appendString:_streetAddress];    }        if (_zip) {        [ret appendFormat:@",%@",_zip];    }        return ret;    }

 :

3、跟蹤使用者位置變化:

MapKit提供了跟蹤使用者位置和方向變化的API,可以不用自己編寫定位服務代碼。開啟地圖視圖的showsUserLocation屬性,並設定方法setUserTrackingMode:就可以了,參考代碼如下:

- (void)viewDidLoad{    [super viewDidLoad];    // 設定地圖的類型    self.mapView.mapType = MKMapTypeStandard;    // 設定代理    self.mapView.delegate = self;        // 允許跟蹤顯示使用者的位置資訊    self.mapView.showsUserLocation = YES;    // 設定使用者跟蹤模式(有3種)    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];}
// 實現地圖視圖的委託方法// 該方法是在定位服務更新完成使用者位置時回調// 在該方法中重新調整地圖的中心點為目前使用者的中心點- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{    self.mapView.centerCoordinate = userLocation.location.coordinate;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.