Change map pin (color or image)
Asked Answered
R

1

0

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;
}
Reparation answered 7/3, 2014 at 19:52 Comment(4)
As you say, there are many questions (and answers) for this (on SO and in the docs). Do you want to set the pin color or do you want to use a custom image? Can you give an example of one of the "various types of code" you tried in viewForAnnotation? Update your question with the code.Atkins
Try this out It will helps to change marker icon [Change Marker Icon][1] [1]:https://mcmap.net/q/282761/-how-to-change-marker-iconTilley
What class did you put the viewForAnnotation method in? Is it in LocationsViewController or MapAnnotation?Atkins
@Anna after reading your post "What class did you put the viewForAnnotation method in? Is it in LocationsViewController or MapAnnotation? –" I moved everything into the LocationViewController and made a slight change in the code and it worked. thank you very much for your help. I am sure i will need it again in the very near future. I posted the code I used in LocationViewController.m up above.Reparation
M
2

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;
}
Mann answered 8/3, 2014 at 0:9 Comment(2)
See the comment and code I added above. SO would not let me post it in a comment as it is to long.Reparation
@pawan please help me on this #35648339Swayder

© 2022 - 2024 — McMap. All rights reserved.