viewforannotation什么时候ios webview js调用oc

后使用快捷导航没有帐号?
暂时没有人问过相似的问题,你可以做第一个提问题的人
查看: 8440|回复: 10
viewForAnnotation不调用
viewForAnnotation不调用,大头针时一直默认的红色,NSLog(@&viewForAnnotation调用了&)没有执行;
我是在真机上测试的
#import &ViewController.h&
#import &BMapKit.h&
@interface ViewController ()
& & BMKPointAnnotation *pointA
& & BMKAnnotationView *newAnnotationV
@implementation ViewController
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
& & self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
& & if(self){}
- (void)viewDidLoad {
& & [super viewDidLoad];
& & /*self.locationManager = [[CLLocationManager alloc]init];
& &&&self.locationManager.delegate =
& &&&self.locationManager.desiredAccuracy =kCLLocationAccuracyB
& &&&[self.locationManager startUpdatingLocation];
& &&&_mapView.showsUserLocation =YES;*/
& & if( ([[[UIDevice currentDevice] systemVersion] doubleValue]&=7.0))
& && &&&self.navigationController.navigationBar.translucent = NO;
& & CLLocationCoordinate2D coors[4] = {0};
& & coors[0].latitude = 39.915;
& & coors[0].longitude = 116.404;
& & coors[1].longitude=116.404;
& & coors[1].latitude =39.815;
& & coors[2].latitude = 39.815;
& & coors[2].longitude = 116.504;
& & coors[3].longitude=116.504;
& & coors[3].latitude =39.915;
& & BMKPolygon *polygon = [BMKPolygon polygonWithCoordinates:coors count:4];
& & [_mapView addOverlay:polygon];//多边形
& & BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coors count:4];
& & [_mapView addOverlay:polyline];//折线
& & BMKCircle *circle =[BMKCircle circleWithCenterCoordinate:coors[0] radius:5000];
& & [_mapView addOverlay:circle];
& & _mapView.showMapScaleBar =
& & _mapView.mapScaleBarPosition = CGPointMake(240, 280);
& & _passPosition =CGPointMake(10, 10);
& & _mapView.zoomEnabled =
& & _mapView.scrollEnabled =
& & _mapView.ChangeWithTouchPointCenterEnabled=
& & if (pointAnnotation == nil) {
& && &&&[self addPointAnnotation];
& & // Do any additional setup after loading the view, typically from a nib.
& & NSLog(@&YES!!!&);
- (IBAction)Button:(id)sender {
& & _imgView.image = [_mapView takeSnapshot];
- (void)didReceiveMemoryWarning {
& & [super didReceiveMemoryWarning];
& & // Dispose of any resources that can be recreated.
-(void)viewWillAppear:(BOOL)animated{
& & [_mapView viewWillAppear];
& & _mapView.delegate =
-(void)viewWillDisappear:(BOOL)animated{
& & [_mapView viewWillDisappear];
& & _mapView.delegate =
-(void)mapView:(BMKMapView *)mapView onClickedMapPoi:(BMKMapPoi *)mapPoi
& & NSLog(@&点到某个点了~~&);
-(void)mapView:(BMKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
& & NSLog(@&区域变了~&);
-(void)mapStatusDidChanged:(BMKMapView *)mapView
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id&BMKOverlay&)overlay{
& & if ([overlay isKindOfClass:[BMKPolyline class]]) {
& && &&&BMKPolylineView *polylineView = [[BMKPolylineView alloc]initWithOverlay:overlay];
& && &&&polylineView.strokeColor = [[UIColor blueColor]colorWithAlphaComponent:1];
& && &&&polylineView.lineWidth =3.0;
& && &&&return polylineV
& & if ([overlay isKindOfClass:[BMKPolygon class]]) {
& && &&&BMKPolygonView *polygonView= [[BMKPolygonView alloc]initWithOverlay:overlay];
& && &&&polygonView.strokeColor = [[UIColor purpleColor]colorWithAlphaComponent:1];
& && &&&polygonView.fillColor = [[UIColor cyanColor]colorWithAlphaComponent:0.2];
& && &&&polygonView.lineWidth =2.0;
& && &&&return polygonV
& & if ([overlay isKindOfClass:[BMKCircle class]]) {
& && &&&BMKCircleView *circleView= [[BMKCircleView alloc]initWithOverlay:overlay];
& && &&&circleView.strokeColor = [[UIColor blueColor]colorWithAlphaComponent:1];
& && &&&circleView.fillColor = [[UIColor cyanColor]colorWithAlphaComponent:0.5];
& && &&&circleView.lineWidth =5.0;
& && &&&return circleV
//添加标注
-(void)addPointAnnotation
& & pointAnnotation = [[BMKPointAnnotation alloc]init];
& & CLLocationCoordinate2D coors2;
& & coors2.latitude = 39.915;
& & coors2.longitude = 116.404;
& & pointAnnotation.coordinate = coors2;
& & pointAnnotation.title = @&test&;
& & pointAnnotation.subtitle=@&这个注释可以拖拽哦!&;
& & [_mapView addAnnotation:pointAnnotation];
#pragma mark -
#pragma mark implement BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id &BMKAnnotation&)annotation
& & NSLog(@&viewForAnnotation调用了&);
& & NSString *annotationViewID = @&renameMark&;
& & if (newAnnotationView == nil) {
& && &&&newAnnotationView = [[BMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationViewID];
& && &&&((BMKPinAnnotationView *)newAnnotationView).pinColor = BMKPinAnnotationColorP
& && &&&((BMKPinAnnotationView *)newAnnotationView).animatesDrop = YES;
& && &&&((BMKPinAnnotationView *)newAnnotationView).draggable = YES;
& & return newAnnotationV
那是因为百度demo的Annotation是后加的
神贴!神贴!神贴!& &重要的话说三遍 我解决了好久,原来是代理没有声明....
我看到个细节。你的addAnnotation实际是在viewDidLoad里被叫的,而你的_mapView.delegate = self是在viewWillAppear里的。viewDidLoad肯定是先于viewWillAppear被叫的。会不会是这个问题?
大牛们快快来
我给你捋一捋思路哦。
你声明的时候newAnnotationView的类是BMKAnnotationView。初始化的时候也是BMKAnnotationView
我没记错的话BMKPinAnnotationView是BMKAnnotationView的子类吧?你把一个父类的对象cast成子类,你觉得可行吗?
至于那句NSLog为什么没有被调用我就真不知道了。
PS:贴代码可以标上是代码吗?这样易读性高一点。
alvisjiang
改了还是不行~~我怀疑是不是bundle文件的问题~
alvisjiang
&&终于OK了。。是这个问题,调用之前委托没有调用。谢谢了~~终于不用纠结这个问题了。。
alvisjiang
& &谢谢大神!!!一样的问题,被你发现了!
回复 5# alvisjiang
你是怎么解决的?可是百度demo就是把代理方法写在viewwillappear里面的呀?求教一下,谢谢
回复 5# alvisjiang
怎么改的,我的怎么没反应,提示更改bundle display name,可是加上这个号就连地图都不显示了
你是怎么解决的?可是百度demo就是把代理方法写在viewwillappear里面的呀?求教一下,谢谢 ...
那是因为百度demo的Annotation是后加的
Powered byiOS 地图SDK | 高德开放平台 | 高德地图API
Android平台
数据管理与开发
高德观景台
您现在的位置: >
> 地图定位
iOS系统不允许第三方定位,定位功能是对iOS系统定位的二次封装。通过封装,可将原始定位点无偏差的显示在高德地图上。
目前,有两种获取当前位置信息的方法:
方法一:使用
目前提供了独立的"iOS定位SDK",无需展示地图即可获取用户位置信息,参考相关介绍
方法二:本文使用地图SDK中的定位功能
展示地图、显示定位标注、支持自定义定位图标和精度圈样式
需在info.plist添加NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription字段
NSLocationWhenInUseUsageDescription表示应用在前台的时候可以搜到更新的位置信息。
NSLocationAlwaysUsageDescription表示应用在前台和后台(suspend或terminated)都可以获取到更新的位置数据。
只要开启定位开关(MAMapView的showsUserLocation属性)就可以开始定位。代码如下所示:
- (void)viewDidLoad
[super viewDidLoad];
_mapView.showsUserLocation = YES;
//YES 为打开定位,NO为关闭定位
当位置更新时,会进定位回调,通过回调函数,能获取到定位点的经纬度坐标,示例代码如下:
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
if(updatingLocation)
//取出当前位置的坐标
NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
将 MAMapView 添加到 Subview 中,开启定位后,会在地图上显示定位图层。高德地图 iOS SDK 支持自定义定位图层的样式。
定位图层有3种显示模式,分别为:
MAUserTrackingModeNone:仅在地图上显示,不跟随用户位置。
MAUserTrackingModeFollow:跟随用户位置移动,并将定位点设置成地图中心点。
MAUserTrackingModeFollowWithHeading:跟随用户的位置和角度移动。
通过以下代码可改变定位图层的显示模式:
[_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES]; //地图跟着位置移动
自定义定位图层
定位图层由定位点处的标注(MAUserLocation)和精度圈(MACircle)组成。
对于3D矢量地图
通过- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id &MAAnnotation&)annotation方法中自定义定位标注样式;
通过- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id &MAOverlay&)overlay方法自定义定位精度圈的样式。
注意:若自定义定位经度圈样式,需将 MAMapView 的customizeUserLocationAccuracyCircleRepresentation 属性设置为 YES。
示例代码如下:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
_mapView.customizeUserLocationAccuracyCircleRepresentation = YES;
_mapView.userTrackingMode = MAUserTrackingModeF
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id &MAOverlay&)overlay
/* 自定义定位精度对应的MACircleView. */
if (overlay == mapView.userLocationAccuracyCircle)
MACircleRenderer *accuracyCircleRenderer = [[MACircleRenderer alloc] initWithCircle:overlay];
accuracyCircleRenderer.lineWidth
accuracyCircleRenderer.strokeColor
= [UIColor lightGrayColor];
accuracyCircleRenderer.fillColor
= [UIColor colorWithRed:1 green:0 blue:0 alpha:.3];
return accuracyCircleR
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id &MAAnnotation&)annotation
/* 自定义userLocation对应的annotationView. */
if ([annotation isKindOfClass:[MAUserLocation class]])
static NSString *userLocationStyleReuseIndetifier = @"userLocationStyleReuseIndetifier";
MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:userLocationStyleReuseIndetifier];
if (annotationView == nil)
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:userLocationStyleReuseIndetifier];
annotationView.image = [UIImage imageNamed:@"userPosition"];
return annotationV
对于2D栅格地图
通过- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views方法自定义定位标注和精度圈的样式。示例代码如下:
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
_mapView.showsUserLocation = YES;
_mapView.userTrackingMode = MAUserTrackingModeFollowWithH
[_mapView setZoomLevel:16.1 animated:YES];
- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views
MAAnnotationView *view = views[0];
// 放到该方法中用以保证userlocation的annotationView已经添加到地图上了。
if ([view.annotation isKindOfClass:[MAUserLocation class]])
MAUserLocationRepresentation *pre = [[MAUserLocationRepresentation alloc] init];
pre.fillColor = [UIColor colorWithRed:0.9 green:0.1 blue:0.1 alpha:0.3];
pre.strokeColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.9 alpha:1.0];
pre.image = [UIImage imageNamed:@"location.png"];
pre.lineWidth = 3;
pre.lineDashPattern = @[@6, @3];
[self.mapView updateUserLocationRepresentation:pre];
view.calloutOffset = CGPointMake(0, 0);
高德地图iOS SDK V2.5.0版本提供后台持续定位的能力,即便你的app退到后台,且位置不变动时,也不会被系统挂起,可持久记录位置信息。该功能适用于记轨迹录或者出行类App司机端。
注意:后台定位必须将info.plist的字段改成NSLocationAlwaysUsageDescription字段。
只需要您的做以下几步操作:
1.添加代码。
_mapView.pausesLocationUpdatesAutomatically = NO;
_mapView.allowsBackgroundLocationUpdates = YES;//iOS9以上系统必须配置
2.工程配置。
a)左侧目录中选中工程名,开启 TARGETS->Capabilities->Background Modes
b)在 Background Modes中勾选 Location updates,如下图所示:
(C) 2002- 版权所有 | 京ICP证070711号
感谢您的反馈
通过该反馈,您可以将对高德开放平台产品和文档的意见和建议发送给我们。如需咨询产品使用问题或获取技术支持,请访问
包含屏幕截图
请选择您要反馈的类型
iOS 地图SDK6799人阅读
iOS Open Platform(5)
今天开始使用百度地图的API实现相关的地理位置功能
在根据官方文档:&
进行一系列的引入后,还需要注意以下两个细节,否则编译和运行时都会出错:
1:让XCode 处于 Objective - C++ 混编模式进行编译: 最简单方法就是:随便更改工程文件中的某一个,将.m更改为.mm .
2:由于静态库里面包含(第四点),所以需要让工程支持类别的编译:&Project-&Build Settings-&Other Linker Flags & 添加值:
&-all_load
3:关于&setPaopaoView 警告 临时解决方案如下: 在&Other Linker Flags新增一个
4:建议合并静态库
接下来开始记录具体用到的功能点:
第一个:成功载入地图后,开启定位功能,确认当前使用设备所在的地理位置,代码如下:
1: 开发定位功能,允许地图应用时时获取地理位置信息,并触发委托.
_mapView.showsUserLocation = YES;//开启定位服务所触发的委托:
- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
}2:如果以上委托被调用,说明位置信息已经成功获取,接下来需要将地图位置,移动到定位所在的位置,代码如下:
_mapView.userLocation //记录设备当前所在位置
NSLog(@&!latitude!!!
%f&,userLocation.location.coordinate.latitude);//经度
NSLog(@&!longtitude!!!
%f&,userLocation.location.coordinate.longitude);//纬度
//传入经纬度,将baiduMapView 锁定到以当前经纬度为中心点的显示区域和合适的显示范围
- (void)setMapRegionWithCoordinate:(CLLocationCoordinate2D)coordinate
BMKCoordinateR
if (!_isSetMapSpan)//这里用一个变量判断一下,只在第一次锁定显示区域时 设置一下显示范围 Map Region
region = BMKCoordinateRegionMake(coordinate, BMKCoordinateSpanMake(0.05, 0.05));//越小地图显示越详细
_isSetMapSpan = YES;
[baiduMapView setRegion:region animated:YES];//执行设定显示范围
_currentSelectCoordinate =
[baiduMapView setCenterCoordinate:coordinate animated:YES];//根据提供的经纬度为中心原点 以动画的形式移动到该区域
执行&setCenterCoordinate:coordinate& &以后开始移动,当移动完成后,会执行以下委托:
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
[baiduMapView.annotations enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
BMKPointAnnotation *item = (BMKPointAnnotation *)
if (item.coordinate.latitude == _currentSelectCoordinate.latitude && item.coordinate.longitude == _currentSelectCoordinate.longitude )
[baiduMapView selectAnnotation:obj animated:YES];//执行之后,会让地图中的标注处于弹出气泡框状态
*stop = YES;
注:0.05 表示显示区域的详细程度,设定的值最小,其显示的地图区域也就更详细,这个自己试试吧. 几句话也描述不清楚.
另外位置信息获取到以后,会不停的去获取,所以不需要使用的时候,把_mapView.showsUserLocation 设置为NO;
第二个:给指定的位置加入标注.
BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
item.coordinate =//经纬度
item.title = titleS
item.subtitle = subTitleS//子标题
[baiduMapView addAnnotation:item];
注:为地图类引用 添加一个 标注 执行 addAnnotation 以后 baiduMapView为触发以下委托,此委托可以定制化标注视图
//原理类似 UITableView 循环委托加载 CellforRowWithIndexPath
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id &BMKAnnotation&)annotation
static NSString *AnnotationViewID = @&annotationViewID&;
BMKAnnotationView *annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&icon_location.png&]];//气泡框左侧显示的View,可自定义
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setFrame:(CGRect){260,0,50,annotationView.Help_height}];
[selectButton setTitle:@&确定& forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView =selectB//气泡框右侧显示的View 可自定义
[selectButton setBackgroundColor:[UIColor redColor]];
[selectButton setShowsTouchWhenHighlighted:YES];
[selectButton addTarget:self action:@selector(Location_selectPointAnnotation:) forControlEvents:UIControlEventTouchUpInside];
//以下三行代码用于将自定义视图和标记绑定,一一对应,目的是当点击,右侧自定义视图时,能够知道点击的是那个标记
annotationView.rightCalloutAccessoryView.tag = _cacheAnnotationT
[_cacheAnnotationMDic setObject:annotation forKey:[NSNumber numberWithInteger:_cacheAnnotationTag]];
_cacheAnnotationTag++;
//如果是我的位置标注,则允许用户拖动改标注视图,并赋予绿色样式 处于
if ([annotation.title isEqualToString:String_myLocation]) {
((BMKPinAnnotationView *)annotationView).pinColor = BMKPinAnnotationColorG//标注呈绿色样式
[annotationView setDraggable:YES];//允许用户拖动
[annotationView setSelected:YES animated:YES];//让标注处于弹出气泡框的状态
((BMKPinAnnotationView *)annotationView).pinColor = BMKPinAnnotationColorR
annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));//不知道干什么用的
annotationView.annotation =//绑定对应的标点经纬度
annotationView.canShowCallout = TRUE;//允许点击弹出气泡框
return annotationV
如果用户手动在地图中点击标注视图或者是 为 标注视图 &BMKAnnotationView setSelect:YES&那么会触发以下委托:
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
_currentSelectCoordinate = view.annotation.
再当点击弹出的气泡框时亦会触发以下委托:
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)
如果要删除标注:
NSMutableArray *annotationMArray = [[NSArray arrayWithArray:baiduMapView.annotations] mutableCopy];
[baiduMapView removeAnnotations:annotationMArray];
第三个就是开始POI检索
baiduMapSearch = [[BMKSearch alloc] init];
baiduMapSearch.delegate =
[BMKSearch setPageCapacity:10]; //设置每次搜索多少页
开始搜索: 提供 搜索 城市 和关键字
BOOL flag = [baiduMapSearch poiSearchInCity:_currentCity withKey:_searchKeywordString pageIndex:_searchPageIndex];
- (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error
//这里判断表示顺利搜索成功
if (error == BMKErrorOk) {
BMKPoiResult* result = [poiResultList objectAtIndex:0];//如果有表示搜索到数据
for (int i = 0; i & result.poiInfoList. i++) {
BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
注:BMKPoiResult 里面展示当前搜索情况,如下:总共搜索到多少条数据,本次搜索到多少条数据,当前搜索到第几页等
& & &BMKPoiInfo 是每一个搜索结果,里面有电话 ,地址,经纬度 等
最多附上相关资源包:&
密码:QNWNaf
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:764467次
积分:6989
积分:6989
排名:第2114名
原创:100篇
评论:150条
Dodo - 极简的计划清单
(1)(1)(1)(1)(1)(1)(3)(1)(2)(3)(2)(2)(4)(3)(1)(8)(7)(10)(12)(10)(16)(7)(1)(1)(4)(1)5542人阅读
iOS开发之实战篇(50)
1. 添加到map view的子视图不会随地图的移动而移动,map view会固定其子视图的位置。如果要添加随着地图移动的子视图,可以使用annotations和overlays。annotation用来显示由一个经纬度定义的位置,而overlay则是由多个点所定义或者包含了许多连续的图形。
2.在地图上显示annotation,需要提供两个对象
annotation object)
annotation view.)
注释对象通常是一些小的数据对象,保存了地图的坐标和一些相关信息。
Map Kit提供了一些标准的注释视图,你也可以使用自定义的注释视图。但是不能将注释视图直接添加到map view,而是使用map view的代理对象来提供。
3.添加注释的具体步骤
定义一个注释对象annotation object :
使用MKPointAnnotation类来实现一个简单的注释,这类注释可以显示标题和副标题。
自定义一个遵守MKAnnotation协议的对象,这类注释可以存储任何类型数据
定义一个注释视图annotation view来显示数据:
如果注释可以由一张静态图片表示,则创建一个MKAnnotationView类的实例,然后将图像赋值给image属性
如果你想使用标准的pin annotation,创建一个MKPinAnnotationView类的实例
如果静态图像不够表示你的注释,那么创建一个MKAnnotationView的子类
Implement the mapView:viewForAnnotation: method in your map view delegate. Your implementation of this method should dequeue an existing annotation view if one exists or create a new one. If your application supports multiple types of annotations, you must
include logic in this method to create a view of the appropriate type for the provided annotation object. 在map view的代理对象中实现mapView:viewForAnnotation:方法,使用已经存在的注释视图或者新创建一个。如果你的程序支持多种不同的注释,那么应该根据不同的注释提供不同的视图
方法添加你的注释对象到map view
4.自定义注释对象
注释对象遵守MKAnnotation协议,如果你之想要将一个标题和一个坐标相联系,那么可以直接使用MKPointAnnotation作为注释对象。如果还想要显示其他信息,就需要自定义一个注释对象
Creating a simple annotation object&&&
@interface MyCustomAnnotation : NSObject &MKAnnotation& {
CLLocationCoordinate2D
@property (nonatomic, readonly) CLLocationCoordinate2D
- (id)initWithLocation:(CLLocationCoordinate2D)
// Other methods and properties.
@implementation MyCustomAnnotation
- (id)initWithLocation:(CLLocationCoordinate2D)coord {
self = [super init];
if (self) {
coordinate =
5.使用标准注释视图
MKAnnotationView类定义了注释视图的一些基本特性。MKPinAnnotationView类是MKAnnotationView的子类,用来显示系统标准的注释视图(pin view)
创建一个MKAnnotationView的实例,设置image属性。当此annotation显示在地图上时,该图像显示在相应的坐标位置(If you do not want the image to be centered on the map coordinate, you can use the centerOffset property to move the center point horizontally and vertically in any direction. )。
MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@&MyCustomAnnotation&] autorelease];
aView.image = [UIImage imageNamed:@&myimage.png&];
aView.centerOffset = CGPointMake(10, -20);
在代理的mapView:viewForAnnotation:方法中创建标准注释视图
6.使用自定义注释视图
#import &UIKit/UIKit.h&
#import &MapKit/MapKit.h&
@interface MyCustomAnnotationView : MKAnnotationView
// Custom data members
// Custom properties and methods.
Initializing a custom annotation view
- (id)initWithAnnotation:(id &MKAnnotation&)annotation reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
// Set the frame size to the appropriate values.
myFrame = self.
myFrame.size.width = 40;
myFrame.size.height = 40;
self.frame = myF
// The opaque property is YES by default. Setting it to
// NO allows map content to show through any unrendered
// parts of your view.
self.opaque = NO;
6.在代理对象中创建注释视图
&Creating annotation views&&&
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id &MKAnnotation&)annotation
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
// Handle any custom annotations.
if ([annotation isKindOfClass:[MyCustomAnnotation class]])
// Try to dequeue an existing pin view first.
MKPinAnnotationView*
pinView = (MKPinAnnotationView*)[mapView
dequeueReusableAnnotationViewWithIdentifier:@&CustomPinAnnotationView&];
if (!pinView)
// If an existing pin view was not available, create one.
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@&CustomPinAnnotation&]
autorelease];
pinView.pinColor = MKPinAnnotationColorR
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
// Add a detail disclosure button to the callout.
UIButton* rightButton = [UIButton buttonWithType:
UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(myShowDetailsMethod:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightB
pinView.annotation =
return pinV
当map view需要显示注释时候,会调用代理的mapView:viewForAnnotation:方法,如果你不实现此方法或者总是返回nil,map view会使用默认的注释视图,即pin annotation view。在你创建新的视图之前,检查是否已经存在此类视图dequeueReusableAnnotationViewWithIdentifier: ,类似于tableview的cell实现
7.管理地图的注释对象
为了避免注释视图的重叠,在方法mapView:regionWillChangeAnimated: andmapView:regionDidChangeAnimated: 中,根据需要来添加或者减少
8.让注释视图可拖动
在注释对象中,实现setCoordinate:方法来更新注释的坐标。
创建注释视图时,设置draggable属性为YES
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:707460次
积分:8053
积分:8053
排名:第1638名
原创:113篇
转载:75篇
评论:97条
(2)(1)(1)(1)(1)(1)(2)(2)(1)(3)(3)(14)(7)(8)(14)(12)(20)(23)(14)(9)(19)(20)(10)}

我要回帖

更多关于 自定义annotationview 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信