在地图视图中显示动态注释图钉
Show dynamic annotation pin in mapview
我正在开发一个使用 MKMapView
的应用程序。我在其中显示了多个注释。现在我想为 pin 显示自定义注释图像。
我为 MapViewAnnotation 定制了 class。
#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 获取多个经纬度并以这种方式显示:
[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;
}
为了显示注释,我正在使用此方法参考 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
条件运行良好,但所有 pin 图像显示是最后一个条件。
让你 for 循环跳出
if ( pinView == nil )
if 块假设只制作可重复使用的注释 pin 并且如果 pin 对象已经存在,将 运行 只制作一次。
我正在开发一个使用 MKMapView
的应用程序。我在其中显示了多个注释。现在我想为 pin 显示自定义注释图像。
我为 MapViewAnnotation 定制了 class。
#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 获取多个经纬度并以这种方式显示:
[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;
}
为了显示注释,我正在使用此方法参考 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
条件运行良好,但所有 pin 图像显示是最后一个条件。
让你 for 循环跳出
if ( pinView == nil )
if 块假设只制作可重复使用的注释 pin 并且如果 pin 对象已经存在,将 运行 只制作一次。