如何在MKMapView中添加不同的图像而不是引脚



我一直在开发一个具有MKMapView的应用程序,我想用不同的图像自定义引脚。我已经这样做了,但只有一个图像,现在我需要让一个别针显示一个图像而另一个别针则显示另一个图像。我该怎么做?如果有帮助,这里是我的代码:

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

pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"image1.jpg"];
}
else {
[_mapView.userLocation setTitle:@"Your Location"];
}
return pinView;   }

我需要第二个和第三个显示相同的图像,但第一个显示不同的图像如下所示:

- (void)viewWillAppear:(BOOL)animated {CLLocationCoordinate2D First; First.latitude = -12.098970; First.longitude = -77.034531; MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; annotationPoint.coordinate = First; annotationPoint.title = @"First"; annotationPoint.subtitle = @"Subtitle1"; [_mapView addAnnotation:annotationPoint];
CLLocationCoordinate2D Second; Second.latitude = -12.098299; Second.longitude = -77.068364; MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init]; annotationPoint2.coordinate = Second; annotationPoint2.title = @"Second"; annotationPoint2.subtitle = @"Subtitle2"; [_mapView addAnnotation:annotationPoint2];
CLLocationCoordinate2D Third; Third.latitude = -12.125888; Third.longitude = -77.023346; MKPointAnnotation *annotationPoint3 = [[MKPointAnnotation alloc] init]; annotationPoint3.coordinate = Third; annotationPoint3.title = @"Third"; annotationPoint3.subtitle = @"Subtitle3"; [_mapView addAnnotation:annotationPoint3];}

确保您的项目中有MapKit.framework和CoreLocation.framework。

我的自定义徽章图像是39高乘32宽。没有尝试过其他尺寸,但可以自由尝试。我的2针图像被称为pin1.png和pin2.png

确保您的图像命名正确,以匹配代码中的内容。

在我的例子中,我没有使用当前位置,而是使用一个静态的自定义位置(我认为巴哈马群岛对这个例子来说会很好)。在你的项目中,你当然会使用位置管理器来获取用户的当前位置。

我已经测试了我的例子,并成功地在地图上放置了2个引脚,每个引脚都有自己的自定义图像。

这不是最干净的代码,但我写它的时间有限

这是ViewController.h的代码

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
@end

这是ViewController.h的代码

#import "ViewController.h"
#import "MyAnnotation.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
@property (strong, nonatomic) IBOutlet MKMapView *myMapView;
@end
@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// setup the map view, delegate and current location
[self.myMapView setDelegate:self];
self.myMapView.mapType = MKMapTypeStandard;
CLLocationCoordinate2D myLocation = CLLocationCoordinate2DMake(25.085130,-77.331428);
[self.myMapView setCenterCoordinate:myLocation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(myLocation, 2000, 2000);
region.center = self.myMapView.centerCoordinate;
self.myMapView.showsUserLocation = YES;
[self.myMapView setRegion:region animated:YES];
[self dropPins];
}
-(void)dropPins {
NSMutableArray *annotationArray = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(25.085130, -77.331428);
MyAnnotation *annotation1 = [[MyAnnotation alloc] initWithCoordinates:location1 image:@"pin1.png"];
[annotationArray addObject:annotation1];
[self.myMapView addAnnotations:annotationArray];
[annotationArray removeAllObjects];
CLLocationCoordinate2D location2 = CLLocationCoordinate2DMake(25.085130, -77.336428);
MyAnnotation *annotation2 = [[MyAnnotation alloc] initWithCoordinates:location2 image:@"pin2.png"];
[annotationArray addObject:annotation2];
[self.myMapView addAnnotations:annotationArray];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else
{
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
if([[(MyAnnotation *)annotationView.annotation image] isEqualToString:@"pin1.png"])
annotationView.image = [UIImage imageNamed:@"pin1.png"];
if([[(MyAnnotation *)annotationView.annotation image] isEqualToString:@"pin2.png"])
annotationView.image = [UIImage imageNamed:@"pin2.png"];
return annotationView;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

这是MyAnnotation.h的代码

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy, readonly) NSString *image;

-(id)initWithCoordinates:(CLLocationCoordinate2D) paramCoordinates
image:(NSString *) paramImage;
@end

这是MyAnnotation.m的代码

#import "MyAnnotation.h"
@implementation MyAnnotation
-(id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates
image:(NSString *)paramImage
{
self = [super init];
if(self != nil)
{
_coordinate = paramCoordinates;
_image = paramImage;
}
return (self);
}
@end

最新更新