地圖-圖釘視圖,地圖圖釘視圖

來源:互聯網
上載者:User

地圖-圖釘視圖,地圖圖釘視圖

////  MKMapViewController.m//  MKMapView////  Created by xiaoyao on 15/3/25.//  Copyright (c) 2015年 lije. All rights reserved.//#import "MKMapViewController.h"#import <CoreLocation/CoreLocation.h>#import <MapKit/MapKit.h>#import "CAnnotation.h"@interface MKMapViewController ()<MKMapViewDelegate,CLLocationManagerDelegate> {  MKMapView *_mapView;  CLLocationManager *_locationManager;}@end@implementation MKMapViewController- (void)viewDidLoad {  [super viewDidLoad] ;    [self setUpUI];}- (void)setUpUI {  CGRect rect = [UIScreen mainScreen].bounds;  _mapView = [[MKMapView alloc] initWithFrame:rect];  _mapView.delegate = self;  [self.view addSubview:_mapView];    if (![CLLocationManager locationServicesEnabled] ||      ![CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {    [_locationManager requestWhenInUseAuthorization];    _locationManager.delegate = self;    [_locationManager startUpdatingLocation];  }    // 設定  _mapView.mapType = MKMapTypeStandard;  // 返回使用者追蹤軌跡,啟動定位  _mapView.userTrackingMode = MKUserTrackingModeFollow;    CAnnotation *ca = [[CAnnotation alloc] init];  CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(36.56, 101.74);  ca.title = @"lije";  ca.subtitle = @"lije’home";  ca.coordinate = coor;  ca.image = [UIImage imageNamed:@"ca1"];  // 添加圖釘  [_mapView addAnnotation:ca];    CAnnotation *ca2 = [[CAnnotation alloc] init];  CLLocationCoordinate2D coor2 = CLLocationCoordinate2DMake(41.31, 121.51);  ca2.title = @"zs";  ca2.subtitle = @"zs’home is jinzhou";  ca2.coordinate = coor2;  ca2.image= [UIImage imageNamed:@"ca2"];  [_mapView addAnnotation:ca2];}#pragma mark - 地圖代理方法- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {  NSLog(@"%@",userLocation);}// 返回圖釘視圖- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {  if ([annotation isKindOfClass:[CAnnotation class]]) {    static NSString *anotationKey = @"Annotation1";    MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:anotationKey];    // 如果緩衝池不存在則建立緩衝池對象    if (!annotationView) {      annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:anotationKey];      annotationView.canShowCallout = true;      UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zs.jpeg"]];      annotationView.leftCalloutAccessoryView = imageView;      annotationView.calloutOffset = CGPointMake(0, 1);    }        annotationView.annotation = annotation;    annotationView.image = ((CAnnotation *)annotation).image;    return annotationView;  } else {        // 如果返回nil則使用預設的圖釘視圖    return nil;  }}@end////  CAnnotation.h//  MKMapView////  Created by xiaoyao on 15/3/25.//  Copyright (c) 2015年 lije. All rights reserved.//#import <Foundation/Foundation.h>#import <MapKit/MapKit.h>@interface CAnnotation : NSObject<MKAnnotation>/** *  @brief 定製圖釘,重寫圖釘協議的三個屬性 */@property (nonatomic, copy)   NSString *title;  // 圖釘標題@property (nonatomic, copy)   NSString *subtitle; // 副標題@property (nonatomic) CLLocationCoordinate2D coordinate; // 位置// 自訂圖釘圖片屬性@property (nonatomic, strong) UIImage *image;@end

聯繫我們

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