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