首页 > 其他 > 详细

地图-大头针视图

时间:2015-03-27 17:26:05      阅读:119      评论:0      收藏:0      [点我收藏+]
//
//  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

地图-大头针视图

原文:http://blog.csdn.net/u010606986/article/details/44678513

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!