Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-22-2009, 11:26 AM   #1 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 1
Default MKAnnotation problem

Due to restrictions in the agreement I am not really sure how much "you can talk" about but the map function has been out in the public so my question is simple, has anyone tried annotations and got it working successfully ?

Last edited by Panterlo; 03-22-2009 at 11:35 AM.
Panterlo is offline   Reply With Quote
Old 03-23-2009, 01:07 AM   #2 (permalink)
New Member
 
Join Date: Nov 2008
Location: BrisVegas
Posts: 105
Send a message via Skype™ to craiglittle
Default

Yes, including the callout views and responding to callout tap events. I would recommend following the Docco rather than relying on sample code, as most of the sample code I have seen has been inaccurate.

One thing I will point out that is unclear in the documentation is creating the view for the callout. The viewForCalloutAccessoryPosition: method can return any type of UIView descendant, but if you want to respond to user taps (via the mapView:annotationView:accessoryButtonTappedAtPosi tion: method) then you have to create it as a UIButton. The docco doesn't explicitly state this, but on my 12th reading I noticed that the docco hints at this.
__________________
The important thing is not so much that every child should be taught, as that every child should be given the wish to learn.
- Sir John Lubbock.
craiglittle is offline   Reply With Quote
Old 03-23-2009, 10:05 AM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 211
Default

Annotations were a cinch to set up. I had to add the annotations to the map on viewDidAppear to get the pin drop animations working:

Code:
- (void) viewDidAppear: (BOOL) animated
{
        [self.mapView addAnnotations: _points];
}
lapse is offline   Reply With Quote
Old 03-24-2009, 08:14 PM   #4 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 6
Default More details?

Quote:
Originally Posted by lapse View Post
Annotations were a cinch to set up. I had to add the annotations to the map on viewDidAppear to get the pin drop animations working:

Code:
- (void) viewDidAppear: (BOOL) animated
{
        [self.mapView addAnnotations: _points];
}
I am able to create an annotation object but I can't make it display on the mapView. The docs say that a new annotationView will be created automatically but how do I link this to my annotation? ANy help much appreciated!
PhilSimpson is offline   Reply With Quote
Old 03-26-2009, 02:50 PM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 211
Default

It's not created automatically. You need to implement this delegate method which will itself be called automatically by the MKMapView:

Code:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation
{
        MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];
        if (pin == nil)
        {
                pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"asdf"] autorelease];
        }
        else
        {
                pin.annotation = annotation;
        }
        pin.pinColor = MKPinAnnotationColorRed;
        pin.animatesDrop = YES;
        return pin;
}
lapse is offline   Reply With Quote
Old 03-28-2009, 07:00 AM   #6 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 6
Default Thanks!

Aha! So like cell views in tables then! That's solved it, thanks very much!

I have noticed one other weirdness though. Using mapView setRegion:animated: seems to cause my map to zoom out to exactly half it's previous zoom level. I'm storing the span value in mapView regionDidChange:animated: and handing it back to mapView setRegion:animated but it seems to ignore me! The idea is that the user can scroll & zoom and when the app moves the map, it retains the user's zoom level.

Anyone else come across this? Any insight on where I'm going wrong?
PhilSimpson is offline   Reply With Quote
Old 04-05-2009, 10:50 AM   #7 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 6
Default Beta 2

Beta 2 seems to have broken pinColors. Mine all appear red now. I noticed that the span issue I was having was listed as being fixed in beta 2, but I'm still getting zooming out when using setRegion.

Anyone?
PhilSimpson is offline   Reply With Quote
Old 04-10-2009, 10:33 PM   #8 (permalink)
New Member
 
Join Date: Nov 2008
Location: BrisVegas
Posts: 105
Send a message via Skype™ to craiglittle
Default

I also had problems getting the location indicator to show properly. It worked in the simulator (showed a blue dot at Cupertino), but no blue dot for my location when I tested on my upgraded iPhone...

It appears that the mapView:viewForAnnotation: delegate method is now called for the location annotation as well as your manually added annotations, a behaviour slightly different from Beta 1. Because I processed it as one of my annotations, it had a red pin on the map instead of a blue dot, and because I have 69 annotations it got lost in the clutter.

I now use the following code (sort of) to determine whether I want to display a pin annotation (for my annotations) or the blue location dot:

Code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
	static NSString *defaultPinID = @"CameraAnnotation";
	MKPinAnnotationView *retval = nil;
	
	if ([annotation isMemberOfClass:[CameraAnnotation class]]) {
		retval = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
		if (retval == nil) {
			retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
		}
		
		if (retval) {
			[retval setPinColor:MKPinAnnotationColorGreen];
			retval.animatesDrop = YES;
			retval.canShowCallout = NO;
		}
	}
	
    return retval;
}
__________________
The important thing is not so much that every child should be taught, as that every child should be given the wish to learn.
- Sir John Lubbock.
craiglittle is offline   Reply With Quote
Old 04-17-2009, 02:25 PM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 10
Default

Quote:
Originally Posted by craiglittle View Post
Yes, including the callout views and responding to callout tap events. I would recommend following the Docco rather than relying on sample code, as most of the sample code I have seen has been inaccurate.

One thing I will point out that is unclear in the documentation is creating the view for the callout. The viewForCalloutAccessoryPosition: method can return any type of UIView descendant, but if you want to respond to user taps (via the mapView:annotationView:accessoryButtonTappedAtPosi tion: method) then you have to create it as a UIButton. The docco doesn't explicitly state this, but on my 12th reading I noticed that the docco hints at this.
The viewForCalloutAccessoryPosition: method doesn't exist in the 3.0 Beta 3 SDK. So my question is, has anyone succeeded in annotating a map in Beta 3? When I call addAnnotation: on the map view, an exception is thrown from fairly deep down in Apple's code. I've filed a bug report.
David Casseres is offline   Reply With Quote
Old 04-17-2009, 04:51 PM   #10 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 6
Default

For what it's worth, my app works under beta 3, but it's not adding accessory stuff to the callouts, it's just showing the pins (purple and green are back) and giving title and subtitle when tapped. Does your app still throw an exception with accessory bits commented out?
PhilSimpson is offline   Reply With Quote
Old 04-18-2009, 10:46 AM   #11 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 10
Default

Quote:
Originally Posted by PhilSimpson View Post
For what it's worth, my app works under beta 3, but it's not adding accessory stuff to the callouts, it's just showing the pins (purple and green are back) and giving title and subtitle when tapped. Does your app still throw an exception with accessory bits commented out?
I haven't even reached the point of trying to do anything with callouts – all I do is create an instance of a very simple class that implements MKAnnotation, and add it to the map view using addAnnotation:. An exception is immediately thrown from from several frames down in the MapKit code, and it looks like an out-of-bounds index for an NSArray is causing it.

I've also tried using an MKPlacemark object (created by a reverse geocoder) instead of my own custom class, and exactly the same thing happens.
David Casseres is offline   Reply With Quote
Old 04-18-2009, 08:29 PM   #12 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 6
Default

Quote:
Originally Posted by David Casseres View Post
I haven't even reached the point of trying to do anything with callouts – all I do is create an instance of a very simple class that implements MKAnnotation, and add it to the map view using addAnnotation:. An exception is immediately thrown from from several frames down in the MapKit code, and it looks like an out-of-bounds index for an NSArray is causing it.

I've also tried using an MKPlacemark object (created by a reverse geocoder) instead of my own custom class, and exactly the same thing happens.
OK, here's my AnnotationListObject.h file:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface AnnotationListObject : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
- (void) moveAnnotation: (CLLocationCoordinate2D) newCoordinate;
@end


And the AnnotationListObject.m file:

#import "AnnotationListObject.h"

@implementation AnnotationListObject

@synthesize coordinate, title, subtitle;

- (void) moveAnnotation: (CLLocationCoordinate2D) newCoordinate {
coordinate = newCoordinate;
}

@end


and the relevant calls from RootViewController:

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: [annotation title]];
if (pin == nil)
{
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: [annotation title]] autorelease];
}
else
{
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorGreen;
pin.animatesDrop = YES;
pin.canShowCallout = TRUE;
return pin;
}

- (void) doAnnotations { // call this from viewDidAppear or somesuch
NSMutableArray annotationList = [NSMutableArray new];
// create loop here if necessary
AnnotationListObject *newAnnotation = [AnnotationListObject new];
CLLocationCoordinate2D tempCoordinate;
tempCoordinate.latitude = 0.0; // set latitude to required value
tempCoordinate.longitude = 0.0; // set longitude to required value
[newAnnotation setCoordinate: tempCoordinate];
[newAnnotation setTitle: @"Title"]; // or whatever
[newAnnotation setSubtitle: @"Subtitle"]; // or whatever
[annotationList addObject: newAnnotation];
newAnnotation release];
// end loop here if looped
[map addAnnotations: annotationList];
}

this is all assuming you have an MKMapView called 'map' correctly connected in Interface Builder. Import the 'AnnotationViewList.h' in the RootViewController file and it should behave. It does for me. Now, if only I could get setRegion:animated: to behave...
PhilSimpson is offline   Reply With Quote
Old 04-22-2009, 07:23 PM   #13 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 10
Default

Quote:
Originally Posted by PhilSimpson View Post
OK, here's my AnnotationListObject.h file:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface AnnotationListObject : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *subtitle;
- (void) moveAnnotation: (CLLocationCoordinate2D) newCoordinate;
@end


And the AnnotationListObject.m file:

#import "AnnotationListObject.h"

@implementation AnnotationListObject

@synthesize coordinate, title, subtitle;

- (void) moveAnnotation: (CLLocationCoordinate2D) newCoordinate {
coordinate = newCoordinate;
}

@end


and the relevant calls from RootViewController:

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: [annotation title]];
if (pin == nil)
{
pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: [annotation title]] autorelease];
}
else
{
pin.annotation = annotation;
}
pin.pinColor = MKPinAnnotationColorGreen;
pin.animatesDrop = YES;
pin.canShowCallout = TRUE;
return pin;
}

- (void) doAnnotations { // call this from viewDidAppear or somesuch
NSMutableArray annotationList = [NSMutableArray new];
// create loop here if necessary
AnnotationListObject *newAnnotation = [AnnotationListObject new];
CLLocationCoordinate2D tempCoordinate;
tempCoordinate.latitude = 0.0; // set latitude to required value
tempCoordinate.longitude = 0.0; // set longitude to required value
[newAnnotation setCoordinate: tempCoordinate];
[newAnnotation setTitle: @"Title"]; // or whatever
[newAnnotation setSubtitle: @"Subtitle"]; // or whatever
[annotationList addObject: newAnnotation];
newAnnotation release];
// end loop here if looped
[map addAnnotations: annotationList];
}

this is all assuming you have an MKMapView called 'map' correctly connected in Interface Builder. Import the 'AnnotationViewList.h' in the RootViewController file and it should behave. It does for me. Now, if only I could get setRegion:animated: to behave...

It turns out my problem was very simple: I was not setting a delegate for the MKMapView. It seems a bit rude for the MapKit to throw an exception for that, but the whole thing was my mistake.

Thanks for your sample code, which may help me even though my problem was something else.
David Casseres is offline   Reply With Quote
Old 05-06-2009, 03:42 PM   #14 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 6
Default

Quote:
Originally Posted by David Casseres View Post
It turns out my problem was very simple: I was not setting a delegate for the MKMapView. It seems a bit rude for the MapKit to throw an exception for that, but the whole thing was my mistake.

Thanks for your sample code, which may help me even though my problem was something else.
You're welcome. For anyone that hasn't tried it yet, beta 4's new MKMapView SetCenterCoordinate:Animated: works a treat - no more unwanted zooming.
PhilSimpson is offline   Reply With Quote
Old 09-17-2009, 10:18 AM   #15 (permalink)
pwb
Registered Member
 
Join Date: Sep 2009
Posts: 2
Default

Quote:
Originally Posted by lapse View Post
It's not created automatically. You need to implement this delegate method which will itself be called automatically by the MKMapView:

Code:
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation
{
        MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];
        if (pin == nil)
        {
                pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"asdf"] autorelease];
        }
        else
        {
                pin.annotation = annotation;
        }
        pin.pinColor = MKPinAnnotationColorRed;
        pin.animatesDrop = YES;
        return pin;
}
Have you seen evidence that AnnotationViews are actually getting placed on the reuse queue? I have code very similar to the above, but placed in NSLog statements to see when I was reusing and creating new annotations. And it never reuses an annotation.
pwb is offline   Reply With Quote
Old 09-17-2009, 10:26 AM   #16 (permalink)
pwb
Registered Member
 
Join Date: Sep 2009
Posts: 2
Default

Quote:
Originally Posted by PhilSimpson View Post
Beta 2 seems to have broken pinColors. Mine all appear red now. I noticed that the span issue I was having was listed as being fixed in beta 2, but I'm still getting zooming out when using setRegion.

Anyone?
I'm replying to Phils comment because I think this code is potentially an answer to why all pins might appear red. I have the following code in my app, and it works. But can anyone tell me why it doesn't result in an infinite loop?

Code:
- (MKAnnotationView *)mapView:(MKMapView *)passedMapView viewForAnnotation:(id <MKAnnotation>)annotation { 
	MKAnnotationView *annotationView; 

	
	if ([annotation class] == [MKUserLocation class] ) { 
		NSLog(@"Detected UserLocationAnnotation"); 
        	annotationView = [passedMapView viewForAnnotation:annotation]; 
		[self doSomeStuffHere];
	} else { 
          // Do other stuff for custom Annotation 
        }
	return annotationView; 
}
pwb is offline   Reply With Quote
Old 02-26-2010, 04:18 PM   #17 (permalink)
Registered Member
 
Join Date: Nov 2009
Age: 23
Posts: 11
Default

Can u help me in my problem.. I have to show 4 lines of data in that bubble..Like One title..then description, date, location name.. All the example showing only 2 lines of details. i tried like appending wanted data in subtitle and seperated by a \n character.. But it didnt worked.. Plz help me
sijo006 is offline   Reply With Quote
Old 02-26-2010, 06:27 PM   #18 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 10
Default

Quote:
Originally Posted by sijo006 View Post
Can u help me in my problem.. I have to show 4 lines of data in that bubble..Like One title..then description, date, location name.. All the example showing only 2 lines of details. i tried like appending wanted data in subtitle and seperated by a \n character.. But it didnt worked.. Plz help me
I wish I had an answer for you. But the MKAnnotation API only allows one string in the title and one in the subtitle.

Anyone else?
David Casseres is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,124
Threads: 38,889
Posts: 170,606
Top Poster: smasher (2,565)
Welcome to our newest member, silveras
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:17 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0