自定义showCallout 左右view
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKAnnotationView *aView=[mapView dequeueReusableAnnotationViewWithIdentifier:@"testMapView"];
if(!aView){
aView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"testMapView"];
//这里是用的MKPinAnnotationView 初始化 当然也可以用MKAnnotationView 只是用MKAnnotationView就必须对MKAnnotationView对象.image赋值 否则 没有图钉显示标记位置
aView.canShowCallout=YES;
//设置是否显示ShowCallout
}
aView.annotation=annotation;
UIImageView *imageV=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//0,0,30,30是组不错的数据。
imageV.image=[UIImage imageNamed:@"Default.png"];
aView.leftCalloutAccessoryView=imageV;
// aView.image=[UIImage imageNamed:@"Default.png"];
// aView.image用于修改在地图上标记的图钉
return aView;
}
实现MKMapViewDelegate协议
并设置当前mapView.delegate
关于annotation
MyAnnotation 用于作为放入MapView annotation数组的对象[放入此数组的id需旅行 <MKAnnotation>协议]
此协议是 readOnly的几个属性的"get"方法实现。因为readOnly 所以无法对其赋值。故传入一个“辞典” 然后在对应的“get”方法中去 词典中去相应的数据。
MyAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
+(MyAnnotation*)initWithNSMutableDictionary:(NSMutableDictionary *)ns;
@end
MyAnnotation.m
#import "MyAnnotation.h"
@interface MyAnnotation()
@property NSMutableDictionary *save;
@end
@implementation MyAnnotation
@synthesize save=_save;
+ (MyAnnotation *)initWithNSMutableDictionary:(NSMutableDictionary *)ns{
MyAnnotation *ma=[[MyAnnotation alloc]init];
ma.save=ns;
return ma;
}
#pragma mark - <Annotation>
-(NSString *)title{
return [self.save objectForKey:@"title"];
}
-(NSString *)subtitle{
return [self.save objectForKey:@"subTitle"];
}
-(CLLocationCoordinate2D)coordinate{
CLLocationCoordinate2D result;
result.latitude=20.0;//纬度
result.longitude=30.0;//经度
return result;
}
@end
VC相关
当mapView或annotations 被设置时则执行更新。刷新界面
保证数据同步展示在屏幕。
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "MyAnnotation.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet MKMapView *myMapView;
@property (strong,nonatomic) NSArray *annotations;
@end
@implementation ViewController
@synthesize myMapView=_myMapView;
@synthesize annotations=_annotations;
-(void)setAnnotations:(NSArray *)annotations{
if(_annotations!=annotations){
_annotations=annotations;
[self update];
}
}
-(void)setMyMapView:(MKMapView *)myMapView{
if(_myMapView!=myMapView){
_myMapView=myMapView;
[self update];
}
}
- (void)update{
if(self.myMapView.annotations){
[self.myMapView removeAnnotations:self.myMapView.annotations];
}
if(self.annotations){
[self.myMapView addAnnotations:self.annotations];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableDictionary *nsTemp=[[NSMutableDictionary alloc]init];
[nsTemp setObject:@"poolo's title" forKey:@"title"];
[nsTemp setObject:@"poolo's SubTitle" forKey:@"subTitle"];
self.annotations=@[[MyAnnotation initWithNSMutableDictionary:nsTemp]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
分享到:
相关推荐
ios源码之google地图demo.rar
iOS 百度地图,定位,用户点击地图添加大头针,自定义大头针,并且可以对大头针的信息增删改
百度地图 iOS SDK是一套基于iOS3.0及以上版本设备的应用程序接口,不仅提供构建地图的基本接口,还提供本地搜索、路线规划、定位等服务。 使用百度地图iOS SDK开发适用于移动设备的地图应用,通过接口,您可以轻松...
ios源码之天气地图Demo.rar
新手会需要的。IOS百度地图经典demo
高德官方地图demo,更进一步的实现可以参考官方文档
完成后台实时定位 记录。 上传位置记录。 查看轨迹 查看附近使用改软件的人。
iOS9 通讯录新框架的操作
iOS,corelocation,定位,通过经纬度计算距离
一个简单的复选框demo,主要是通过设置button的image来 做的
基于Git上的MAImage lib及Tesseract-OCR-iOS lib整合起来的OCR识别Demo, http://blog.csdn.net/ouq68/article/details/44015483
ios 操作通讯录联系人demo大全
IOS 单例设计模式实例Demo 单例 设计 模式 IOS Singleton
iOS打开相机、相册Demo
实现iOS地图定位功能,制作的的小demo,其中涵盖的功能基本地图定位.
iOS 个人中心 demo 用collectionView做的
ios xmpp demo
IOS 设计模式抽象工厂实例Demo IOS 设计 模式 抽象 工厂
ios 调起地图(百度、高德、系统自带)App导航 demo,是一个展示地图位置标注,公交、自驾、步行3种路线规划的类。并且能同时选择性地调起百度、高德、系统自带高德地图 App 客户端进行全功能的导航功能。使用方法简单...
这是一个关于弹出提示的消息(动画)框架,可以直接使用!