在地图视图中显示动态注释图钉 [英] Show dynamic annotation pin in mapview

查看:79
本文介绍了在地图视图中显示动态注释图钉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用MKMapView的应用程序.我在其中显示了多个注释.现在,我想显示图钉的自定义注释图像. 我为MapViewAnnotation制作了自定义类.

I am developing an app which use MKMapView. I have display multiple annotation in it. Now I want to display custom annotation image for pin. I have made custom class for MapViewAnnotation.

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapViewAnnotation : NSObject <MKAnnotation>

@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id) initWithTitle:(NSString *) title SubTitle:(NSString *)subtitle AndCoordinate:(CLLocationCoordinate2D)coordinate;

@end


#import "MapViewAnnotation.h"

@implementation MapViewAnnotation

@synthesize coordinate=_coordinate;
@synthesize title=_title;

-(id) initWithTitle:(NSString *) title SubTitle:(NSString *)subtitle AndCoordinate:(CLLocationCoordinate2D)coordinate;
{
    self =  [super init];
    _title = title;
    _coordinate = coordinate;
    _subtitle = subtitle;
    return self;
}

@end

我从api获得多个经纬度,并以此方式显示:

I am getting multiple lat-long from api and displaying by this way:

[self.map_parking addAnnotations:[self createAnnotations]];


- (NSMutableArray *)createAnnotations
{
    NSMutableArray *annotations = [[NSMutableArray alloc] init];

    for (NSDictionary *row in arrfinal) {
        NSNumber *latitude = [row objectForKey:@"latitude"];
        NSNumber *longitude = [row objectForKey:@"longitude"];
        NSString *title = [row objectForKey:@"title"];
        NSString *subtitle = [row objectForKey:@"subtitle"];
        NSString *rate = [row objectForKey:@"rate"];

        //Create coordinates from the latitude and longitude values
        CLLocationCoordinate2D coord;
        coord.latitude = latitude.doubleValue;
        coord.longitude = longitude.doubleValue;

        MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:title SubTitle:subtitle AndCoordinate:coord];

        MKAnnotationView *av = [map_parking viewForAnnotation:annotation];
        if ([rate isEqualToString:@"0.00"])
        {
            av.image = [UIImage imageNamed:@"marker2"];
        }
        else
        {
            av.image = [UIImage imageNamed:@"marker"];
        }
        [annotations addObject:annotation];

    }
    return annotations;
}

要显示注释,我通过以下链接使用此方法参考: MKMapView:自定义视图代替了注解针

To Display annotation I am using this method reference by this link: MKMapView: Instead of Annotation Pin, a custom view

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKAnnotationView *pinView = nil;
    if(annotation != map_parking.userLocation)
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKAnnotationView *)[map_parking dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil )
            pinView = [[MKAnnotationView alloc]
                       initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.canShowCallout = YES;
        //pinView.animatesDrop = YES;
        for (int i=0; i<arrrates.count; i++)
        {
            if ([[arrrates objectAtIndex:i] isEqualToString:@"0.00"])
            {
                pinView.image = [UIImage imageNamed:@"marker1"];
            }
            else if ([[arrrates objectAtIndex:i] isEqualToString:@"100.71"])
            {
                pinView.image = [UIImage imageNamed:@"marker2"];
            }
            else
            {
                pinView.image = [UIImage imageNamed:@"marker3"];
            }
        }

    }
    else {
        [map_parking.userLocation setTitle:@"I am here"];
    }

    return pinView;
}

在此方法中,if-else条件工作正常,但所有引脚图像显示都是最后的条件.

In this method if-else condition working good but all pin image display which was the last condition.

推荐答案

让您退出

if ( pinView == nil )

如果块假定仅使可重复使用的注释图钉&如果pin对象已经存在,它将仅运行一次.

if block suppose to make reusable annotation pin only & will run only once if the pin object already exist.

这篇关于在地图视图中显示动态注释图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆