xcode5.0
1.新建一个空工程
//
// MainViewController.m
// my_mapDemo
//
// Created by fenghuo on 14-7-30.
// Copyright (c) 2014年 fenghuo. All rights reserved.
//
#import "MainViewController.h"
#import <MapKit/MapKit.h>
#import "WXAnnotation.h"
@interface MainViewController () <MKMapViewDelegate>
@property MKMapView *mapView;
@end
@implementation MainViewController
@synthesize mapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
self.mapView.delegate = self;
[self.view addSubview:self.mapView];
// 是否显示当前设备的位置
self.mapView.showsUserLocation = YES;
// 地图初始位置
// [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(39.904299,116.22169)];
CLLocationCoordinate2D coord = {39.904299,116.22169}; // 经纬度
MKCoordinateSpan span = {0.1, 0.1}; // 显示的返回,数值越大,范围就越大
MKCoordinateRegion region = {coord, span};
//地图初始化的时候显示的区域
[self.mapView setRegion:region];
// 创建anation对象
CLLocationCoordinate2D showCoord = {39.904299,116.22169};
WXAnnotation *anation1 = [[WXAnnotation alloc] initWithCoordinate2D:showCoord];
anation1.title = @"万达电影院";
anation1.subtitle = @"小标题";
// 创建anation对象
CLLocationCoordinate2D showCoord2 = {39.804299,116.32169};
WXAnnotation *anation2 = [[WXAnnotation alloc] initWithCoordinate2D:showCoord2];
anation2.title = @"万达电影院2";
anation2.subtitle = @"小标题2";
[self.mapView addAnnotation:anation1];
[self.mapView addAnnotation:anation2];
// 长按手势,切换地图类型;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(updateMapType:)];
longPress.minimumPressDuration = 2.0f;
[self.view addGestureRecognizer:longPress];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)updateMapType:(UILongPressGestureRecognizer *)_longPress
{
// 避免两次调用;
if (_longPress.state == UIGestureRecognizerStateEnded) {
return;
}
if(self.mapView.mapType == MKMapTypeStandard){
self.mapView.mapType = MKMapTypeSatellite; // 卫星地图
}
else if (self.mapView.mapType == MKMapTypeSatellite)
{
self.mapView.mapType = MKMapTypeHybrid; //混合地图
}
else{
self.mapView.mapType = MKMapTypeStandard; // 标准地图
}
}
#pragma mark - MKAnnotationView delegate
- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
NSLog(@"viewForAnnotation");
/*
//-------------------创建大头针视图---------------------
static NSString *identifier = @"Annotation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
// MKPinAnnotationView 是大头针视图
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
annotationView.canShowCallout = YES;
annotationView.annotation = annotation;
// 设置大头针的颜色
annotationView.pinColor = MKPinAnnotationColorRed;
// 从天上落下的动画
annotationView.animatesDrop = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
//标题右边视图
annotationView.rightCalloutAccessoryView = button;
return annotationView;
*/
//-------------------使用图片作为标注视图---------------------
static NSString *identifier = @"Annotation";
MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}
annotationView.image = [UIImage imageNamed:@"play"];
//设置是否显示标题视图
annotationView.canShowCallout = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
//标题右边视图
// annotationView.rightCalloutAccessoryView = button;
return annotationView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(@"didSelectAnnotationView");
}
- (void)mapView:(MKMapView *)_mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
NSLog(@"didDeselectAnnotationView");
}
- (void)buttonAction
{
NSLog(@"显示电影院详情");
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface WXAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
- (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate;
@end
#import "WXAnnotation.h"
@implementation WXAnnotation
- (id)initWithCoordinate2D:(CLLocationCoordinate2D)coordinate {
self = [super init];
if (self != nil) {
_coordinate = coordinate;
}
return self;
}
@end