第一步。
首先需要自定义一个类,这里叫它PoiAnnotation,采用MKAnnotation协议。
这个类的对象用来存放小气球的坐标信息和显示文字。
第二步。完善MapviewController类,就是那个含有你mapview的类。
第三步。添加MapKit.framework到工程里面来。
好了,运行下试试吧。如有疏漏 欢迎大家批评指出。运行效果图如下:)
首先需要自定义一个类,这里叫它PoiAnnotation,采用MKAnnotation协议。
这个类的对象用来存放小气球的坐标信息和显示文字。
代码如下:
/* PoiAnnotation.h */
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface PoiAnnotation : NSObject<MKAnnotation>
{
// 坐标
CLLocationCoordinate2D _coordinate;
// 小标题和大标题
NSString *_subtitle;
NSString *_title;
}
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic,copy) NSString *title;
-(id) initWithCoords:(CLLocationCoordinate2D) coords;
@end
/* PoiAnnotation.m */
#import "PoiAnnotation.h"
@implementation PoiAnnotation
@synthesize coordinate = _coordinate;
@synthesize subtitle = _subtitle;
@synthesize title = _title;
- (id) initWithCoords:(CLLocationCoordinate2D) coords{
self = [super init];
if (self != nil) {
_coordinate = coords;
}
return self;
}
- (void) dealloc
{
[_title release];
[_subtitle release];
[super dealloc];
}
@end
第二步。完善MapviewController类,就是那个含有你mapview的类。
/* MapviewController.h */
#import <UIKit/UIKit.h>
#import "MapKit/Mapkit.h"
#import "PoiAnnotation.h"
@interface MapViewController : UIViewController<MKMapViewDelegate>
{
// 地图视图
IBOutlet MKMapView *_mapBankView;
PoiAnnotation *_annotation;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapBankView;
@end
/* MapviewController.m */
@implementation MapViewController
@synthesize mapBankView = _mapBankView ;
- (void)viewDidLoad
{
[super viewDidLoad];
_mapBankView.delegate = self ;
[self create];
}
//设置小气球的属性
-(void)create
{
//经纬度
double dLatitude = 39.9 ;
double dLongitude = 116.46 ;
//地图显示的中心区域
MKCoordinateRegion newRegion;
newRegion.center.latitude = dLatitude;
newRegion.center.longitude = dLongitude;
// 显示范围
newRegion.span.latitudeDelta = 0.1;
newRegion.span.longitudeDelta = 0.1;
// 设置地图显示的中心位置
[_mapBankView setRegion:newRegion animated:YES];
// 以下设置小气球的相关信息,经纬度,显示内容等
CLLocationCoordinate2D p1;
PoiAnnotation *poi;
p1.latitude=dLatitude;
p1.longitude = dLongitude;
poi = [[PoiAnnotation alloc] initWithCoords:p1];
poi.title=@"MapView";
poi.subtitle=@"Test !!!!";
[_mapBankView addAnnotation:poi];
[poi release];
}
//根据annotation生成对应的View
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
newAnnotation.pinColor = MKPinAnnotationColorRed;
// 小气球从最上方滑落
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout=YES;
// 点击小气球弹出来的view里面的button
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
newAnnotation.rightCalloutAccessoryView=button;
return newAnnotation;
}
-(void)btnClicked
{
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
第三步。添加MapKit.framework到工程里面来。
好了,运行下试试吧。如有疏漏 欢迎大家批评指出。运行效果图如下:)