 |
 |
|
 |
03-22-2009, 11:26 AM
|
#1 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 1
|
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.
|
|
|
03-23-2009, 01:07 AM
|
#2 (permalink)
|
|
New Member
Join Date: Nov 2008
Location: BrisVegas
Posts: 105
|
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.
|
|
|
03-23-2009, 10:05 AM
|
#3 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 211
|
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];
}
|
|
|
03-24-2009, 08:14 PM
|
#4 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 6
|
More details?
Quote:
Originally Posted by lapse
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!
|
|
|
03-26-2009, 02:50 PM
|
#5 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 211
|
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;
}
|
|
|
03-28-2009, 07:00 AM
|
#6 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 6
|
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?
|
|
|
04-05-2009, 10:50 AM
|
#7 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 6
|
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?
|
|
|
04-10-2009, 10:33 PM
|
#8 (permalink)
|
|
New Member
Join Date: Nov 2008
Location: BrisVegas
Posts: 105
|
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.
|
|
|
04-17-2009, 02:25 PM
|
#9 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 10
|
Quote:
Originally Posted by craiglittle
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.
|
|
|
04-17-2009, 04:51 PM
|
#10 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 6
|
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?
|
|
|
04-18-2009, 10:46 AM
|
#11 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 10
|
Quote:
Originally Posted by PhilSimpson
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.
|
|
|
04-18-2009, 08:29 PM
|
#12 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 6
|
Quote:
Originally Posted by David Casseres
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...
|
|
|
04-22-2009, 07:23 PM
|
#13 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 10
|
Quote:
Originally Posted by PhilSimpson
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.
|
|
|
05-06-2009, 03:42 PM
|
#14 (permalink)
|
|
New Member
Join Date: Mar 2009
Posts: 6
|
Quote:
Originally Posted by David Casseres
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.
|
|
|
09-17-2009, 10:18 AM
|
#15 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 2
|
Quote:
Originally Posted by lapse
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.
|
|
|
09-17-2009, 10:26 AM
|
#16 (permalink)
|
|
Registered Member
Join Date: Sep 2009
Posts: 2
|
Quote:
Originally Posted by PhilSimpson
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;
}
|
|
|
02-26-2010, 04:18 PM
|
#17 (permalink)
|
|
Registered Member
Join Date: Nov 2009
Age: 23
Posts: 11
|
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
|
|
|
02-26-2010, 06:27 PM
|
#18 (permalink)
|
|
Registered Member
Join Date: Nov 2008
Posts: 10
|
Quote:
Originally Posted by sijo006
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?
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 505 |
| 52 members and 453 guests |
| aderrington, AdyCoder, albi, Alexman, bensj, BrianSlick, CHV, DanLyons, davejas69, develsolutions, digidan, doblezeta, earblast, ethanwa, Exaviorn, gc2010, gomoliako, Guit, h3sperian, harrytheshark, hazelvine, helen412, hunt19, iSpiderman, iTrackiGiveaway, jaywright00, johnblack45, jojo453, Kalimba, kalygraphix, Kryckter, LemonMeringue, masc2279, mbc230, mgilani, miguel.campiao, moonshiner, pereorra, Reyna, Rudy, saul102, sayer, shan, silveras, sorghum, stuffradio, Tambourin, techdigm, thomasosterling, vertine, walkman2001, warmi |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,124
Threads: 38,889
Posts: 170,606
Top Poster: smasher (2,565)
|
| Welcome to our newest member, silveras |
|