Change map pin (color or image)

孤街醉人 提交于 2020-01-06 08:27:30

问题


I know this is a question asked a lot. I have been reading through many of the questions on here regarding this for a while. I was hoping someone would have a simpler way using the code I have to replace the pins on my map with an image. I am still very new to objective c.The code I have is very short and easy. Most of what I have found online to post pins to a map is very long and drawn out. I have the following classes and controllers for my map view:

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

I have tried to implement methods similar to this:

 -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{.......

with various types of code but to no avail. Not sure what I am doing wrong. Isn't there a shorter way to do what I am thinking than this than what I have seen so far online.

I was hoping there was something similar to :

MapAnnotation *pinWI = [[[MapAnnotation alloc] initWithCoordinate: wisconsin] initWithPinImage:@"imagename.png"];

or could I implement something similar to this: pinWI:MKPinAnnotationColorGreen; only with a image location defined?

or something along those lines. Thanks in advance for everyones help and patience with a newbie.

I really need to change the pin to a custom image. One coding schema I tried was this:

    - (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;
}

WORKING CODE:

- (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;
}

回答1:


add the type property(example-firstPin, secondPin,thirdPin etc) to your custom MapViewAnnotation class.

MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init...
newAnnotation.type = @"firstPin";  // <-- set property in annotation
[self.mapView addAnnotation:newAnnotation];

and in your - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation method put a simple check

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;
}


来源:https://stackoverflow.com/questions/22259427/change-map-pin-color-or-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!