首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mkmapview MKUserLocation AnnotationView

mkmapview MKUserLocation AnnotationView
EN

Stack Overflow用户
提问于 2013-03-19 08:47:15
回答 4查看 6K关注 0票数 3

我试图为我的地图上的注释创建自定义注释视图。我是通过修改协议MKMapViewDelegate和覆盖函数mapView:viewForAnnotation:来做到这一点的。这一切都有效,唯一的问题是我还将showsUserLocation设置为TRUE,这意味着我在mapView:viewForAnnotation:方法中得到的一个“注释”是类MKUserLocation

我不想让userlocation注释有我的自定义注释视图,我希望那个注释视图显示默认的userlocation注释视图!如何返回用户位置的默认用户位置注释视图或将其排除在注释(mapView:viewForAnnotation:中的注释中)之外?

我试图在UserLocation方法中捕获mapView:viewForAnnotation:,但是我不知道该返回什么!(在本例中,我将返回一个标准的MKAnnotationView,但这看起来并不像默认的UserLocation注释(显然是)。

代码语言:javascript
复制
    if (![[annotation class] isEqual:[MKUserLocation class]]) {

        MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customAnnotation"];
        // edit the custom view
        return view;
    }

    MKAnnotationView *view = [[MKAnnotationView alloc] init];
    return view;
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-19 09:06:41

要显示用户位置的默认注释,只需在这种情况下返回nil,我是这样做的:

代码语言:javascript
复制
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // use your custom annotation
    if ([annotation isKindOfClass:[MyAnnotationClass class]]) {
        ...

        return annotationView;
    }

    // use default annotation
    return nil;
}
票数 8
EN

Stack Overflow用户

发布于 2013-03-19 09:04:32

在viewForAnnotation方法中编写这段代码。在这里,var 'map‘是您的MKMapview的出口;

代码语言:javascript
复制
   - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
    {



        //Annoation View for current Location

        if(map.userLocation != annotation)

        {
            UIImage *image = [UIImage imageNamed:@"image.png"];
            annotation.image = image;

            return annotation; 


        }

        //Annotation View for current location

         return nil;

    }
票数 2
EN

Stack Overflow用户

发布于 2013-03-19 09:05:29

创建自定义AnnotationView

代码语言:javascript
复制
#import <MapKit/MapKit.h>

@interface AnnotationView : MKPlacemark

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;

// you can put here any controllers that you want. (such like UIImage, UIView,...etc)

@end

.m file

代码语言:javascript
复制
#import "AnnotationView.h"

@implementation AnnotationView

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
    {
        self.coordinate = coordinate;
    }
    return self;
}

@end

//使用注释在相关的#import "AnnotationView.h"中添加.m file

代码语言:javascript
复制
CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;

// Create Obj Of  AnnotationView class  

AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;

    annotation.title = @"I m Here";
    annotation.subtitle = @"This is Sub Tiitle";

[self.mapView addAnnotation:annotation];

上面的AnnotationView**.**是如何创建的简单示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15494887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档