一:Cocoa Touch 没有提供地图注解类,只定义了一个 MKAnnotation协议。要创建地图注解,必须设计符合
PinView.m
三,添加大头钉,并显示和对大头钉的操作.
MapViewController.m
PinView.h
//
// PinView.h
// MapLocation
//
// Created by skychi on 13-5-6.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface PinView : NSObject<MKAnnotation>{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
PinView.m
//
// PinView.m
// MapLocation
//
// Created by skychi on 13-5-6.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import "PinView.h"
@implementation PinView
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
-(void) dealloc
{
self.title = nil;
self.subtitle = nil;
[super dealloc];
}
@end
二,定义一个简单跳转视图.
ViewController.h
//
// ViewController.h
// MapLocation
//
// Created by skychi on 13-5-3.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>{
CLLocationCoordinate2D coordinate2d;
}
@end
ViewController.m
//
// ViewController.m
// MapLocation
//
// Created by skychi on 13-5-3.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import "ViewController.h"
#import "MapViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc]initWithFrame:self.view.frame];
[label setText:@"向左滑动来显示您当前的位置"];
[label setTextAlignment:NSTextAlignmentCenter];
[label setTextColor:[UIColor redColor]];
[label setFont:[UIFont systemFontOfSize:18.0f]];
[self.view addSubview:label];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(loadLocation)];
recognizer.direction =UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:recognizer];
[recognizer release];
CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器
locationManager.delegate=self;//设置代理
locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别
locationManager.distanceFilter=1000.0f;//设置距离筛选器
[locationManager startUpdatingLocation];//启动位置管理器
// Do any additional setup after loading the view, typically from a nib.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
coordinate2d = newLocation.coordinate;
NSLog(@"%f,%f",coordinate2d.latitude,coordinate2d.longitude);
}
-(void)loadLocation{
MapViewController *mapView = [[MapViewController alloc]initWithNibName:@"MapViewController" bundle:[NSBundle mainBundle]];
mapView.coordinate2d = coordinate2d;
[self.navigationController pushViewController:mapView animated:YES];
[mapView release];
}
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
三,添加大头钉,并显示和对大头钉的操作.
MapViewController.h
//
// MapViewController.h
// MapLocation
//
// Created by skychi on 13-5-3.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>
@property (retain, nonatomic) IBOutlet MKMapView *myMapView;
@property (assign, nonatomic) CLLocationCoordinate2D coordinate2d ;
@end
MapViewController.m
//
// MapViewController.m
// MapLocation
//
// Created by skychi on 13-5-3.
// Copyright (c) 2013年 skychi. All rights reserved.
//
#import "MapViewController.h"
#import "PinView.h"
@interface MapViewController ()
@end
@implementation MapViewController
@synthesize coordinate2d;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.myMapView.delegate=self;
MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };
theRegion.center=self.coordinate2d;
[self.myMapView setZoomEnabled:YES];
[self.myMapView setScrollEnabled:YES];
theRegion.span.longitudeDelta = 0.01f;
theRegion.span.latitudeDelta = 0.01f;
[self.myMapView setRegion:theRegion animated:YES];
PinView *pinView = [[PinView alloc] init];
pinView.title = @"Title";
pinView.subtitle = @"SubTitle";
//地点名字
pinView.coordinate = theRegion.center;
[self.myMapView addAnnotation:pinView]; // Do any additional setup after loading the view from its nib.
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *indentifer = @"QuanFangPinID";//一个重用的标识往下看会明白的
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)//显示用户位置的那个蓝点也是大头针,我不希望改变它,所以在这里要判断一下
{
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: indentifer];//熟悉吧,像不想tableview的重用机制,没错,地图也会重用大头针的
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: indentifer] autorelease];//到这里一个空的MKPinAnnotationView对象基本初始化好了,可是如果仅是这样没有意义,我们要把它变成我们想的样子
for (UIView * view in [pinView subviews])
{
[view removeFromSuperview];
}
pinView.image = nil;//这是大头针的样子,我们设为空,在后面会在大头针上添加自己定义的视图,当然你可以在这里定义自己的样子
pinView.canShowCallout = YES;//在点击大头针的时候会弹出那个黑框框,看看我上面发的图就知道了,这个就是决定能不能弹出的。
pinView.animatesDrop = NO;//地图在添加大头针时的会自带下落动画,这里可以决定用不用,如果不用,你可以自己定义其它的动画,下面我会介绍
pinView.draggable = NO;//这里决定pinView能不能拖动,应该经常会把它设为NO
/*下面就是我自定义视图,然后把它们add到pinView上去*/
UIImage *image = [UIImage imageNamed:@"mark.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[pinView addSubview: imageView];
[imageView release];
/*下面这个东西需要仔细看一下,创建了一个button,但是要看看button为什么这样加上去,它的位置就是那个蓝色的指向按钮,当然我们并不一定要创建button,任何继承于UIView的视图都可以,点击的时候会调到一个委托方法,- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; (如果你创建了一个button,你可能会自己制订方法和button联系起来,但是我建议最好不要这么做,这个委托会很好的代替它,并且把可能用到的信息提供给你)*/
UIImage *leftImage = [UIImage imageNamed:@"options"];
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0, 0, leftImage.size.width, leftImage.size.height);
[leftButton setBackgroundImage:leftImage forState:UIControlStateNormal];
leftButton.tag = 1 ;
pinView.leftCalloutAccessoryView = leftButton;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];;
rightButton.tag = 2;
pinView.rightCalloutAccessoryView = rightButton;
}
return pinView;
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
if ([control isKindOfClass:[UIButton class]]) {
if (control.tag == 1) {
[self showAlertView:@"LeftButton" AndMessage:view.annotation.title];
NSLog(@"%@",view.annotation.title) ;
}else if (control.tag == 2){
[self showAlertView:@"RightButton" AndMessage:view.annotation.subtitle];
NSLog(@"%@",view.annotation.subtitle) ;
}
}
}
-(void)showAlertView:(NSString *)title AndMessage :(NSString *)message{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@ is pressed!",title] message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil];
[alertView show];
[alertView release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_myMapView release];
[super dealloc];
}
@end
;
实现效果如下: