我知道这是一个很多问题。我一直在阅读有关此问题的许多问题。我希望有人能用更简单的方法使用我的代码来替换地图上的图钉。我对目标c仍然很新。我的代码非常简短。我在网上找到的大多数将引脚发布到地图上的内容都非常长而且很长。我的地图视图有以下类和控制器:
LocationsViewController.h
#import "TechBookAppDelegate.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationsViewController : UIViewController <MKMapViewDelegate>
{
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
@end
LocationsViewController.m
#import "LocationsViewController.h"
#import "MapAnnotation.h"
@interface LocationsViewController ()
@end
@implementation LocationsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CLLocationCoordinate2D wisconsin = {.latitude = xx.xxxxxxx, .longitude = -xx.xxxxxx};
CLLocationCoordinate2D pennsyvainia = {.latitude = xx.xxxxxxx, .longitude = -xx.xxxxxx};
CLLocationCoordinate2D nevada = {.latitude = xx.xxxxxxx, .longitude = -xx.xxxxxx};
CLLocationCoordinate2D texas = {.latitude = xx.xxxxxxx, .longitude = -xx.xxxxxx};
MapAnnotation *pinWI = [[MapAnnotation alloc] initWithCoordinate: wisconsin];
MapAnnotation *pinPA = [[MapAnnotation alloc] initWithCoordinate: pennsyvainia];
MapAnnotation *pinNV = [[MapAnnotation alloc] initWithCoordinate: nevada];
MapAnnotation *pinTX = [[MapAnnotation alloc] initWithCoordinate: texas];
[worldView addAnnotation:pinTX];
[worldView addAnnotation:pinNV];
[worldView addAnnotation:pinWI];
[worldView addAnnotation:pinPA];
}
MapAnnotation.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D _coordinate;
MKAnnotationView *mapView;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
MapAnnotation.m
#import "MapAnnotation.h"
#import "LocationsViewController.h"
@implementation MapAnnotation
@synthesize coordinate = _coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self != nil)
{
_coordinate = coordinate;
}
return self;
}
@end
我试图实现与此类似的方法:
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{.......
有各种类型的代码但无济于事。不确定我做错了什么。与我迄今为止在网上看到的相比,是不是有更短的方式来做我想的事情。
我希望有类似的东西:
MapAnnotation *pinWI = [[[MapAnnotation alloc] initWithCoordinate: wisconsin] initWithPinImage:@"imagename.png"];
或者我可以实现与此类似的东西: pinWI:MKPinAnnotationColorGreen; 只有定义了图像位置?
或类似的东西。提前感谢每个人对新手的帮助和耐心。
我真的需要将引脚更改为自定义图像。我尝试过的一种编码模式是:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:@"location.png"];
annotationView.annotation = annotation;
return annotationView;
}
工作代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[worldView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
annotationView.image = [UIImage imageNamed:@"locations_marker.png"];
annotationView.annotation = annotation;
return annotationView;
}
答案 0 :(得分:2)
将类型属性(example-firstPin,secondPin,thirdPin等)添加到自定义MapViewAnnotation
类。
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init...
newAnnotation.type = @"firstPin"; // <-- set property in annotation
[self.mapView addAnnotation:newAnnotation];
并在您的- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
方法中进行简单检查
MapViewAnnotation *mvAnn = (MapViewAnnotation *)annotation;
if ([mvAnn.type isEqualToString:@"firstPin"])
{
annView.pinColor = MKPinAnnotationColorGreen;
}
else if ([mvAnn.type isEqualToString:@"secondPin"])
{
annView.pinColor = MKPinAnnotationColorRed;
}
else if ([mvAnn.type isEqualToString:@"thirdPin"])
{
annView.pinColor = MKPinAnnotationColorOrange;
}