Change map output (color or image)

I know this question is asked very often. Over the years, I read many of these questions. I was hoping someone would have an easier way to use the code, which I have to replace on my image card. I am still very new to object c. The code I have is very short and easy. Most of what I found online to send messages to the card is very long and elongated. 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 tried to implement methods like this:

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

with different types of code, but to no avail. Not sure what I'm doing wrong. Is there no shorter way to do what I think is than this than what I have seen so far on the Internet.

I was hoping there is something similar to:

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

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

or something like that. Thank you in advance for the help and patience of all the newcomers.

I really need to change the output to a custom image. One coding scheme I tried was the following:

  - (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; } 
0
ios objective-c mkmapview mkannotationview
Mar 07 '14 at 19:52
source share
1 answer

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

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

and in your method - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation enter 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; } 
+2
Mar 08 '14 at 0:09
source share



All Articles