Different MKPinAnnotation for different data - objective-c

I have 3 types of locations.
I'm getting the data from my server, then I'm parsing it and displaying the locations on the mapView.
I want to display different colors for the different types of data. 3 types = 3 colors.
How can I control this?

Implement the viewForAnnotation delegate method for doing this.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[yourAnnotationLocation class]])
{
MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
//if you need image you can set it like
//annotationView.image = [UIImage imageNamed:#"yourImage.png"];//here we use a nice image instead of the default pins
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
annotationView.annotation = annotation;
}
if ([annotation.title isEqualToString:#"Midhun"])
{
annotationView.pinColor = MKPinAnnotationColorGreen;
}
else
{
annotationView.pinColor = MKPinAnnotationColorRed;
}
return annotationView;
}
return nil;
}
For setting custom property to your annotation add a class which confirms to MKAnnotation protocol.
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyLocation : NSObject <MKAnnotation> {
NSString *_name;
NSString *_address;
int _yourValue;
CLLocationCoordinate2D _coordinate;
}
#property (copy) NSString *name;
#property (copy) NSString *address;
#property (assign) yourValue;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
#end
This is is a nice tutorial.

Try this,
annotation1.subtitle = #"1st annotation";
annotation2.subtitle = #"2st annotation";
annotation3.subtitle = #"3st annotation";
Check annotation
if ([annotation.subtitle isEqualToString:#"1st annotation"])
{
//change color
}
else if ([annotation.subtitle isEqualToString:#"2st annotation"])
{
//change color
}
else if ([annotation.subtitle isEqualToString:#"3st annotation"])
{
//change color
}

You can do something with latitude and longitude comparison
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *identifier = #"yourIdentifier";
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if([annotation coordinate].latitude==YourLocationLatitude)
{
pin.image=[UIImage imageNamed:#"Flag-red.png"];
}
else
{
pin.image=[UIImage imageNamed:#"Flag-green.png"];
}}

Related

Unable to get custom MKAnnotationView callout to appear

I'm building an OSX application that uses Mapkit, and I'm trying to get a callout to appear when I click on an MKAnnotationView on my map. To do this I'm implementing the MKMapViewDelegate, and the following function :
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[LocationPin class]]) {
LocationPin *returnPin = (LocationPin *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"LocationPin"];
if (!returnPin){
returnPin = [LocationPin createLocationPinForMapView:mapView annotation:annotation];
}
returnPin.title = annotation.title;
NSButton *rightButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 100.0, 80.0)];
[rightButton setTitle:#"Info"];
[rightButton setBezelStyle:NSShadowlessSquareBezelStyle];
returnPin.annotation = annotation;
returnPin.canShowCallout = YES;
returnPin.rightCalloutAccessoryView = rightButton;
return returnPin;
}
return nil;
}
The function runs fine everytime I put a new pin down, and I made sure the titles of the pins are not empty or null, but the callout still is not showing up. Anybody have any ideas?
EDIT:
After looking through Anna's response, I realized I misunderstood how to implement the MKAnnotation protocol. I've deleted my LocationPin class, which inherited from MKAnnotationView, and instead added the following class to represent my custom MKAnnotation, with a class inside of it to generate a custom MKAnnotationView:
#implementation LocationAnnotation:
-(id)initWithTitle:(NSString *)newTitle Location:(CLLocationCoordinate2D)location {
self = [super init];
if(self) {
_title = newTitle;
_coordinate = location;
}
return self;
}
- (MKAnnotationView *)annotationView {
MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:#"LocAnno"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [NSImage imageNamed:#"dvd"];
NSButton *rightButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 100.0, 80.0)];
[rightButton setTitle:#"Info"];
[rightButton setBezelStyle:NSShadowlessSquareBezelStyle];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
#end
I've also changed the MKMapViewDelegate function the following way now:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[LocationAnnotation class]]) {
LocationAnnotation *anno = (LocationAnnotation *)annotation;
MKAnnotationView *returnPin = [mapView dequeueReusableAnnotationViewWithIdentifier:#"LocAnno"];
if (!returnPin){
returnPin = anno.annotationView;
}
else {
returnPin.annotation = annotation;
}
return returnPin;
}
return nil;
}
But the callout still is not appearing. Any help is appreciated.
Edit 2:
As requested, LocationAnnotation.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface LocationAnnotation : NSObject <MKAnnotation>
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (copy, nonatomic) NSString *title;
-(id)initWithTitle:(NSString *)newTitle Location:(CLLocationCoordinate2D)location;
-(MKAnnotationView *)annotationView;
#end
I figured out what was wrong. I was subclassing MKMapView, which is apparently not recommended by Apple, as it can cause some unwanted behavior.

Custom annotation with custom variable

I currently have a map view controller in which i add annotations to based on some parsed json data.
I'm trying to pass a value from this json data to the following segue and they way i want to do this is to add a custom variable to each annotation (venueId) so when it is pressed i can set a global value that the next segue gets via some logic.
However everything i have tried has resulted in a NUll value for the venueId, my code is as follows:
MyLocation.H
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyLocation : NSObject <MKAnnotation>{
NSNumber *venueId;
}
#property (nonatomic,readonly) NSNumber *venueId;
- (id)initWithName:(NSString *)name address:(NSString *)address coordinate:(CLLocationCoordinate2D)coordinate venueId:(NSNumber*)venueId;
#end
MyLocation.M
#import "MyLocation.h"
#import "GlobalData.h"
#interface MyLocation ()
#property (nonatomic,copy) NSString *name;
#property (nonatomic,copy) NSString *address;
#property (nonatomic,assign) CLLocationCoordinate2D coordinate;
#end
#implementation MyLocation
#synthesize venueId = _venueId;
- (id) initWithName:(NSString *)name address:(NSString *)address coordinate:(CLLocationCoordinate2D)coordinate venueId:(NSNumber*)venueId{
if ((self = [super init])) {
self.name = name;
self.address = address;
self.coordinate = coordinate;
_venueId = self.venueId;
}
return self;
}
- (NSString *)title{
return _name;
}
- (NSString *)subtitle{
return _address;
}
- (CLLocationCoordinate2D)coordinate{
return _coordinate;
}
- (NSNumber *)venueId{
return _venueId;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
NSLog(#"THE CALL OUT has been pressed");
}
#end
PLotting the venues in my MapViewController.M
// plot venues based on data passed in
- (void)plotVenuePositions{
for (id<MKAnnotation> annotation in _mapView.annotations){
// start fresh and remove any annotations in the view
[_mapView removeAnnotation:annotation];
}
for (NSDictionary *row in [[GlobalData sharedGlobalData]venuesArray]){
NSNumber *venueId = [row objectForKey:#"v_id"];
NSString *venueName =[row objectForKey:#"v_name"];
NSNumber *venueLat = [row objectForKey:#"v_lat"];
NSNumber *venueLon = [row objectForKey:#"v_lon"];
NSString *venueTown = [row objectForKey:#"t_name"];
// create co-ord
CLLocationCoordinate2D coordinate;
// set values
coordinate.latitude = venueLat.doubleValue;
coordinate.longitude = venueLon.doubleValue;
// create annotation instance
MyLocation *annotation = [[MyLocation alloc]initWithName:venueName address:venueTown coordinate:coordinate venueId:venueId];
// add annotation
[_mapView addAnnotation:annotation];
NSLog(#"VNEU ID IS %#",venueId);
NSLog(#"ANNOTATION name is %#", annotation.title);
NSLog(#"ANNOTATION subtitle is %#", annotation.subtitle);
NSLog(#"ANNOTATION description is %#", annotation.description);
NSLog(#"ANNOTATION venue ID IS %#", (MyLocation *)annotation.venueId);
}
}
And finally the annotation checks in MapViewController.M
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:#"pin_orange.png"];
// set the cell to have a callout button
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else{
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
In the initWithName method, this line:
_venueId = self.venueId;
does not set the current location's venue id to the init method's parameter value venueId.
It sets it to the current location's property value for venueId.
Since that property value is backed by _venueId, the line is effectively setting it equal to itself and since it is initially null, it stays null.
The line should be:
_venueId = venueId;
or if you are not using ARC:
_venueId = [venueId retain];
This line will, however, give you a compiler warning that the "local variable hides the instance variable". This is because the method parameter name is the same as the property name.
Although the change will work, to remove the compiler warning, change the name of the method parameter to something other than venueId (eg. iVenueId) and then the changed line would be:
_venueId = iVenueId;

Using MKMapViewDelegate

I can't understand why it's not working.
I have a map with a marker, I would like to change the icon
map.h:
#define METERS_PER_MILE 1609.344
#interface Map : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>{
IBOutlet MKMapView *map;
CLLocationManager *locationManager;
}
#property(nonatomic,retain) IBOutlet MKMapView *map;
#property(nonatomic,retain)IBOutlet UIWindow *window;
#property (nonatomic, readwrite) CLLocationCoordinate2D location;
- (void)setMarkers:(MKMapView *)mv;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
#end
And in map.m this:
#implementation Map
#synthesize map, window, location;
- (void)setMarkers:(MKMapView *)mv
{
//no necesary here
}
- (void)viewWillAppear:(BOOL)animated {
// 1
location.latitude = 38.989567;
location.longitude= -1.856283;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.8*METERS_PER_MILE, 0.8*METERS_PER_MILE);
// 3
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
// 4
[map setRegion:adjustedRegion animated:YES];
//[self setMarkers: map];
CLLocationCoordinate2D pointCoord = location;
NSString *theTitle = #"title";
NSString *theSubtitle = #"subtitle";
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:pointCoord title:theTitle subTitle:theSubtitle];
[map addAnnotation:mp];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
NSLog(#"here!!");
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image=[UIImage imageNamed:#"Icon.png"];//here we use a nice image instead of the default pins
return annotationView;
}
return nil;
}
#end
The viewForAnnotation is never executed (I ckeck this with NSLog)
What should I do?
Thank you in advance
Looks like there's nothing wrong with your code. As phix23 said, you may missed to link the MKMapView with the delegate:

MKMapview place pin at location (long/lat)

I have latitude and long values and I need to be able to drop a pin at this location.
Can anybody provide some advice on how to go about this?
Find the below very simple solution to drop the pin at given location define by CLLocationCoordinate2D
Drop Pin on MKMapView
Edited:
CLLocationCoordinate2D ctrpoint;
ctrpoint.latitude = 53.58448;
ctrpoint.longitude =-8.93772;
AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:ctrpoint];
[mapview addAnnotation:addAnnotation];
[addAnnotation release];
You should:
1. add the MapKit framework to your project.
2. create a class which implements the MKAnnotation protocol.
Sample:
Annotation.h
#interface Annotation : NSObject <MKAnnotation> {
NSString *_title;
NSString *_subtitle;
CLLocationCoordinate2D _coordinate;
}
// Getters and setters
- (void)setTitle:(NSString *)title;
- (void)setSubtitle:(NSString *)subtitle;
#end
Annotation.m
#implementation Annotation
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[self setTitle:nil];
[self setSubtitle:nil];
[super dealloc];
}
#pragma mark -
#pragma mark Getters and setters
- (NSString *)title {
return _title;
}
- (NSString *)subtitle {
return _subtitle;
}
- (void)setTitle:(NSString *)title {
if (_title != title) {
[_title release];
_title = [title retain];
}
}
- (void)setSubtitle:(NSString *)subtitle {
if (_subtitle != subtitle) {
[_subtitle release];
_subtitle = [subtitle retain];
}
}
- (CLLocationCoordinate2D)coordinate {
return _coordinate;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
_coordinate = newCoordinate;
}
#end
2. create an instance of this class and set the lat/lon property
3. add the instance to the MKMapView object with this method:
- (void)addAnnotation:(id<MKAnnotation>)annotation
4. You should set the delegate of the map and implement the following method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
static NSString* ShopAnnotationIdentifier = #"shopAnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ShopAnnotationIdentifier];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ShopAnnotationIdentifier] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
}
return pinView;
}
This assumes that you have ARC enabled and that you have included the MapKit framework.
First create a class that implements the MKAnnotation protocol. We'll call it MapPinAnnotation.
MapPinAnnotation.h
#interface MapPinAnnotation : NSObject <MKAnnotation>
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly) NSString* title;
#property (nonatomic, readonly) NSString* subtitle;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location
placeName:(NSString *)placeName
description:(NSString *)description;
#end
MapPinAnnotation.m
#implementation MapPinAnnotation
#synthesize coordinate;
#synthesize title;
#synthesize subtitle;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location
placeName:(NSString *)placeName
description:(NSString *)description;
{
self = [super init];
if (self)
{
coordinate = location;
title = placeName;
subtitle = description;
}
return self;
}
#end
Then add the annotation to the map using the following:
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496,
-119.70182);
MapPinAnnotation* pinAnnotation =
[[MapPinAnnotation alloc] initWithCoordinates:coordinate
placeName:nil
description:nil];
[mMapView addAnnotation:pinAnnotation];
The containing class will have to implement the MKMapViewDelegate protocol. In particular you will have to define the following function to draw the pin:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString* myIdentifier = #"myIndentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];
if (!pinView)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = NO;
}
return pinView;
}
In this example the MKAnnotation title and subtitle member variables are not used, but they can be displayed in the delegate function.
-(MKPointAnnotation *)showClusterPoint:(CLLocationCoordinate2D)coords withPos:(NSString *)place
{
float zoomLevel = 0.5;
region = MKCoordinateRegionMake (coords, MKCoordinateSpanMake (zoomLevel, zoomLevel));
[mapView setRegion: [mapView regionThatFits: region] animated: YES];
point = [[MKPointAnnotation alloc]init];
point.coordinate = coords;
point.title=place;
[mapView addAnnotation:point];
return point;
}
Swift Version
let location = CLLocationCoordinate2DMake(13.724362, 100.515342);
let region = MKCoordinateRegionMakeWithDistance(location, 500.0, 700.0)
self.mkMapView.setRegion(region, animated: true)
// Drop a pin
let dropPin = MKPointAnnotation();
dropPin.coordinate = location;
dropPin.title = "Le Normandie Restaurant";
self.mkMapView.addAnnotation(dropPin);
Please use this code. its working fine.
-(void)addAllPinsOnMapView
{
MKCoordinateRegion region = mapViewOffer.region;
region.center = CLLocationCoordinate2DMake(23.0225, 72.5714);
region.span.longitudeDelta= 0.1f;
region.span.latitudeDelta= 0.1f;
[mapViewOffer setRegion:region animated:YES];
mapViewOffer.delegate=self;
MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.0225, 72.5714);
mapPin.title = #"Title";
mapPin.coordinate = coordinate;
[mapViewOffer addAnnotation:mapPin];
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:
(MKAnnotationView *)view
{
NSLog(#"%#",view.annotation.title);
NSLog(#"%f",view.annotation.coordinate.latitude);
NSLog(#"%f",view.annotation.coordinate.longitude);
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
((MKUserLocation *)annotation).title = #"Current Location";
return nil;
}
else
{
MKAnnotationView *pinView = nil;
static NSString *defaultPinID = #"annotationViewID";
pinView = (MKAnnotationView *)[mapViewOffer dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ){
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
}
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"placeholder"];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// [infoButton addTarget:self action:#selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = infoButton;
pinView.rightCalloutAccessoryView.tag=1;
return pinView;
}
}
- (MKPinAnnotationView*)myMap:(MKMapView*)myMap viewForAnnotation:
(id<MKAnnotation>)annotation{
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"CustomPin"];
UIImage *icon = [UIImage imageNamed:#"bustour.png"];
UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(8,0,32,37)];
if(icon == nil)
NSLog(#"image: ");
else
NSLog(#"image: %#", (NSString*)icon.description);
[iconView setImage:icon];
[pin addSubview:iconView];
pin.canShowCallout = YES;
pin.pinColor = MKPinAnnotationColorPurple;
return pin;
}
- (IBAction)btnLocateMe:(UIButton *)sender
{
[mapViewOffer setCenterCoordinate:mapViewOffer.userLocation.location.coordinate animated:YES];
}

Why is MKMapView delegate method not being called?

I'm very new to iphone development and I'm trying to annotate a map. I've been able to get 3 location points on a map with hardcoded coordinates and now I'm trying to customize the pins. I know to do that you need to implement the:-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation { delegate method.
For some reason I've been unable to even get it called. Here is what I have in the controller header:
#import "ArtPiece.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface TestViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapView;
NSMutableArray *mapAnnotations;
float results_lat[3];
float results_long[3];
NSMutableArray *results_title;
NSMutableArray *Arts;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
#property (nonatomic, retain) NSMutableArray *mapAnnotations;
#end
And here is what I have in the controller implementation:
- (void)viewDidLoad {
results_long[0] = -122.477989;
results_lat[0] = 37.810000;
results_long[1] = -122.480000;
results_lat[1] = 37.820000;
results_long[2] = -122.4850000;
results_lat[2] = 37.830000;
self.mapAnnotations = [[NSMutableArray alloc] initWithCapacity:3];
Arts = [[NSMutableArray alloc] initWithCapacity:3];
for(int x = 0; x<3; x++)
{
// creates an ArtPiece object and sets its lat and long
ArtPiece *a = [[ArtPiece alloc] init];
a.longitude = [NSNumber numberWithDouble:results_long[x]];
a.latitude = [NSNumber numberWithDouble:results_lat[x]];
a.title = #"please show up";
// add objects to annotation array
[self.mapAnnotations insertObject:a atIndex:x];
[a release];
}
// center screen on cali area
[self gotoLocation];
for(int x = 0; x<3; x++)
{
[mapView addAnnotations:mapAnnotations];
}
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
NSLog(#"This is not printing to to console...");
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
And here is the ArtPiece class implementation:
#import "TestViewController.h"
#import "ArtPiece.h"
#import <MapKit/MapKit.h>
#import <CoreData/CoreData.h>
#implementation ArtPiece
#synthesize title, artist, series, description, latitude, longitude;
- (CLLocationCoordinate2D)coordinate
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = [self.latitude doubleValue];
theCoordinate.longitude = [self.longitude doubleValue];
return theCoordinate;
}
#end
It's probably something simple. Is there something wrong with my method declaration? Did I declare the delegate wrong? I can't figure it out, again, I'm very new to this objective c/delegate stuff. Thanks for any help.
The addAnnotations method expects an NSArray of objects that conform to the MKAnnotation protocol.
You are adding objects of type ArtPiece which don't seem to implement MKAnnotation which has a coordinate property (not latitude and longitude properties).
Update your ArtPiece class to conform to MKAnnotation (or use the pre-defined MKPointAnnotation class). But updating your custom class is a better fix.