一个简单的MapView例子。气球坠落

第一步。
首先需要自定义一个类,这里叫它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到工程里面来。

好了,运行下试试吧。如有疏漏 欢迎大家批评指出。运行效果图如下:)


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值