I think those two are related and I'm probably doing something wrong here:
First I'm overriding viewForAnnotation - in order to put my custom Image
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// we avoid changing the user's location blue point
if([annotation isKindOfClass: [MKUserLocation class]]) {
return nil;
}
else
{
static NSString *SFAnnotationIdentifier = #"SFAnnotationIdentifier";
MKPinAnnotationView* pinAnnotationView = (MKPinAnnotationView*)[self.mapToPin dequeueReusableAnnotationViewWithIdentifier:SFAnnotationIdentifier];
if (!pinAnnotationView)
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier];
UIImage *littleImage = [UIImage imageNamed:#"littleImage.png"];
// You may need to resize the image here.
annotationView.image = littleImage;
pinAnnotationView.draggable = true;
pinAnnotationView.canShowCallout = true;
pinAnnotationView.animatesDrop = true;
return annotationView;
}
else
{
pinAnnotationView.annotation = annotation;
}
return pinAnnotationView;
}
}
It seems that pinAnnotationView's draggable, canShowCallout and animatesDrop are not working.
Then, I'm calling my method in my viewDidLoad to set the Annotation
-(void) setAnnotation:(NSString*)lat : (NSString*)lng
{
_mapAnnotation = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D coordinateForPin;
coordinateForPin.latitude = [lat floatValue];
coordinateForPin.longitude = [lng floatValue];
[_mapAnnotation setCoordinate:coordinateForPin];
[_mapAnnotation setTitle:#"sometitle"];
[_mapToPin addAnnotation:_mapAnnotation];
}
It seems that [_mapAnnotation setTitle:#"sometitle"]; is not working anymore when I'm overriding viewForAnnotation.
Is this the right way to go? I have been browsing but didn't find any solution that could help. Any feedback would be appreciated.
The problem is the accidental reference to the wrong variable name. The pinAnnotationView is nil inside that if statement, so setting properties with that nil reference is a NOOP.
if (!pinAnnotationView) {
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:SFAnnotationIdentifier];
...
annotationView.draggable = true; // not pinAnnotationView
annotationView.canShowCallout = true;
annotationView.animatesDrop = true;
return annotationView;
}
FWIW, if targeting iOS 10 or later, you can register your own class and move your annotation view configuration code into that subclass. Furthermore, in iOS 11 and later you do not need mapView:viewForAnnotation: at all.
For example, in iOS 11 and later:
// MyAnnotationView.h
#import MapKit;
NS_ASSUME_NONNULL_BEGIN
#interface MyAnnotationView : MKPinAnnotationView
#end
NS_ASSUME_NONNULL_END
and
// MyAnnotationView.m
#import "MyAnnotationView.h"
#implementation MyAnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
...
self.draggable = true;
self.canShowCallout = true;
self.animatesDrop = true;
}
return self;
}
#end
And then, in your viewDidLoad, you register this class.
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView registerClass:[MyAnnotationView class] forAnnotationViewWithReuseIdentifier:MKMapViewDefaultAnnotationViewReuseIdentifier];
...
}
You can then remove the mapView:viewForAnnotation: implementation entirely.
Related
Setting up a map view with annotation of nearby cafes.
But when I try to custom the annotations using MKMarkerAnnotationView, nothing shows on the map view.
and when I log the marker, the frame is (0 0; 0 0);
I tried pin too, still didn't work.
I set up delegate on storyboard already.
I also debugged the view controller there are marker views but it is not displaying them because the width and height are zero ?
- (void)viewDidLoad {
[super viewDidLoad];
[self.cafeMap setShowsUserLocation:YES];
[self.locationManager requestWhenInUseAuthorization];
self.cafes = [NSMutableArray array];
[self fetchData];
[self.cafeMap registerClass:[MKAnnotationView class] forAnnotationViewWithReuseIdentifier:#"marker"];
}
-(void)fetchData{
[self.networkManager fetchCafeData:^(NSArray * _Nonnull businesses) {
for (NSDictionary* cafeInfo in businesses) {
Cafe *cafe = [[Cafe alloc]initWithCafeInfo:cafeInfo];
[self.cafes addObject:cafe];
}
for (Cafe *cafe in self.cafes) {
[self.cafeMap addAnnotation:cafe];
}
[self.cafeMap showAnnotations:self.cafes animated:YES];
}];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
MKMarkerAnnotationView *marker = [[MKMarkerAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"marker"];
marker = (MKMarkerAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:#"marker" forAnnotation:annotation];
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
marker.rightCalloutAccessoryView = button;
[marker setEnabled:YES];
[marker setCanShowCallout:YES];
NSLog(#"MARKER:%#",marker);
return marker;
}
This is the output:
MARKER:<MKAnnotationView: 0x7f9fa3f333b0; frame = (0 0; 0 0); layer = <CALayer: 0x60000253d220>>
Note that your message says that it is a MKAnnotationView. That is because you have registered MKAnnotationView for your identifier rather than MKMarkerAnnotationView. You want:
[self.cafeMap registerClass:[MKMarkerAnnotationView class] forAnnotationViewWithReuseIdentifier:#"marker"];
As an aside, you should be able to simplify viewForAnnotation to:
MKAnnotationView *marker = [mapView dequeueReusableAnnotationViewWithIdentifier:#"marker" forAnnotation:annotation];
Personally, I’d move the configuration of the annotation view into its own subclass:
static NSString * const cafeClusteringIdentifier = #"cafe";
#interface CafeAnnotationView: MKMarkerAnnotationView
#end
#implementation CafeAnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
self.rightCalloutAccessoryView = button;
self.canShowCallout = true;
self.clusteringIdentifier = cafeClusteringIdentifier;
}
return self;
}
- (void)setAnnotation:(id<MKAnnotation>)annotation {
[super setAnnotation:annotation];
self.clusteringIdentifier = cafeClusteringIdentifier;
}
#end
By doing this, I avoid bloating my view controller with code for configuring annotation views.
Note, I’m setting the clusteringIdentifier so that you enjoy that behavior of the MKMarkerAnnotationView.
And then I’d register that class for MKMapViewDefaultAnnotationViewReuseIdentifier:
[self.cafeMap registerClass:[CafeAnnotationView class] forAnnotationViewWithReuseIdentifier:MKMapViewDefaultAnnotationViewReuseIdentifier];
The benefit of using MKMapViewDefaultAnnotationViewReuseIdentifier is that you don’t have to implement viewForAnnotation at all. Delete your implementation of that method entirely. In iOS 11 and later, you only need to implement viewForAnnotation if you need to do something special like having multiple custom reuse identifiers for multiple types of annotations.
Anyway, that yields:
Helloall in MapKit I can't figure out how to get the animateDrop animation to run, I'm using 1 viewController with both CoreLocation and MapKit running on it.
I read that I need to make sure I have set a delegate from the mapview to self in viewDidLoad which I have done and also to make sure I am using MKPinAnnotationView to get access to the animateDrop property, which I think I have done also?
I'm building for iOS8 also.
Any help would be great
//I'm not showing all code just showing some of the related parts of code below
.h
//I do set standard imports UIKit, CoreLocation, MapKit etc
#interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
//I do set various properties related. CLLocationManager, CLLocation, MKMapView
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
myAnnotation.coordinate = CLLocationCoordinate2DMake(37.813186, 144.962979);
myAnnotation.title = #"My title";
myAnnotation.subtitle = #"My sub title";
[self.mapView addAnnotation:myAnnotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// Handle any custom annotations.
if ([annotation isKindOfClass:[MKPointAnnotation class]])
{
// Try to dequeue an existing pin view first.
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomPinAnnotationView"];
//pinView.animatesDrop = YES;
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
Any help would be great
Environment
Xcode: 5.0.2, Device: iPhone,
iOS: iOS 7
I am trying to use the mapView:viewForAnnotation: delegate method. Within this method, if I create the MKAnnotationView object, the pin gets displayed without any issue. Here is the working code:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[CustomeAnnotation class]])
{
// CustomeAnnotation *myLocation = (CustomeAnnotation *)annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MyCustomAnnotation"];
if (annotationView == nil)
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MyCustomAnnotation"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
//annotationView.image = [UIImage imageNamed:#"park_icon"];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
// annotationView = myLocation.createAnnotationView;
else
annotationView.annotation = annotation;
//return nil;
return annotationView;
}
else
return nil;
}
When I create a class method and move the MKAnnotationView object creation and property setting within the class method and I call it from the mapView:viewForAnnotation: delegate method, the pin does not appear.
Here is the code for the two methods in question (mapView:viewForAnnotation: and createAnnotationView):
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.delegate = self;
CLLocationCoordinate2D baysideParkCoordinates = CLLocationCoordinate2DMake(25.774407, -80.185797);
CustomeAnnotation *baysideParkAnnotation = [[CustomeAnnotation alloc] initWithTitle:#" Bayfront Park"
coordinate:baysideParkCoordinates];
[self.mapView addAnnotation:baysideParkAnnotation];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[CustomeAnnotation class]])
{
CustomeAnnotation *myLocation = (CustomeAnnotation *)annotation;
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MyCustomAnnotation"];
if (annotationView == nil)
annotationView = [myLocation createAnnotationView];
else
annotationView.annotation = annotation;
// return nil;
return annotationView;
}
else
return nil;
}
#end
The custom class
#import "CustomeAnnotation.h"
#implementation CustomeAnnotation
-(id)initWithTitle:(NSString *)newTitle coordinate:(CLLocationCoordinate2D) newCoordinate
{
self = [super init];
if (self)
{
self.title = newTitle;
self.coordinate = newCoordinate;
}
return self;
}
-(MKAnnotationView *)createAnnotationView
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:#"MyCustomAnnotation"];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
//annotationView.image = [UIImage imageNamed:#"park_icon"];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
#end
The import statement for the CustomeAnnotation class is included in the ViewController.h file.
At this point I believe I am not passing back correctly the MKAnnotationView object back to the method call in the ViewController implementation file. Could anyone tell me what is it that I am doing wrong on the second set of code?
In the second set of code, in createAnnotationView, the annotation is not showing because it's not setting the image.
Note that an MKAnnotationView does not have a default image so if you don't set it, the annotation is invisible.
Uncomment the setting of the image (or create an MKPinAnnotationView instead).
The reason the first set of code works (even though it is also creating an MKAnnotationView) is because there's actually a small bug in the code:
MKAnnotationView *annotationView = [mapView dequeue...
if (annotationView == nil)
{
MKAnnotationView *annotationView = [[MKAnnotationView alloc] init...
//^^^^^^^^^^^^^^^^ Here, the code declares a NEW, LOCAL variable
// named annotationView but it has no connection to the
// annotationView declared outside the if-block.
Because the annotationView declared outside the if is never set, it stays nil and that's what the delegate method actually returns.
When you return nil from viewForAnnotation, the map view creates a default view for you (a red pin for your annotations, a blue dot for the user location).
To fix the first set of code, don't declare a new, local variable. Just set the variable:
MKAnnotationView *annotationView = [mapView dequeue...
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] init...
and don't forget to set the image (or create an MKPinAnnotationView instead).
I've written some code for showing annotations with custom images in a mapview.
My mapview delegate implements this method to customize annotation when they are put in the map:
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation {
if ([annotation isKindOfClass:[Station class]]) {
Station *current = (Station *)annotation;
MKPinAnnotationView *customPinview = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
if([[current type] compare:FONTANELLA]==NSOrderedSame)
customPinview.pinColor = MKPinAnnotationColorPurple;
else{
int test=current.bici;
if(test==0)
customPinview.image = [UIImage imageNamed:#"bicimir.png"];
else if(test<4)
customPinview.image = [UIImage imageNamed:#"bicimi.png"];
else if(test>=4)
customPinview.image = [UIImage imageNamed:#"bicimig.png"];
}
customPinview.animatesDrop = NO;
customPinview.canShowCallout = YES;
return customPinview;
}
else{
NSString *identifier=#"MyLocation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
return annotationView;
}
}
The problem is a strange behavior when I long click on a custom annotation on the map:
The image change and the default red pin is shown.
Why this behavior? And How can I avoid it?
When you want to use a custom image for an annotation view, create a generic MKAnnotationView instead of an MKPinAnnotationView.
The MKPinAnnotationView really likes to display its default image which is a pin.
Rearrange the logic a bit so that for FONTANELLA, it creates an MKPinAnnotationView but for the rest an MKAnnotationView.
Also, you should really implement annotation view re-use for all cases (and the last else part doesn't make sense since nothing is done if the dequeue doesn't return anything--you could just do return nil; instead).
inside .h file
#interface AddressAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *mPinColor;
}
#property (nonatomic, retain) NSString *mPinColor;
#end
in .m file
#implementation AddressAnnotation
#synthesize coordinate mPinColor;
- (NSString *)pincolor{
return mPinColor;
}
- (void) setpincolor:(NSString*) String1{
mPinColor = String1;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
#end
inside .m class file
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(AddressAnnotation *) annotation{
UIImage *anImage=[[UIImage alloc] init];
MKAnnotationView *annView=(MKAnnotationView*)[mapView1 dequeueReusableAnnotationViewWithIdentifier:#"annotation"];
if(annView==nil)
{
annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation"] autorelease];
}
if([annotation.mPinColor isEqualToString:#"green"])
{
anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Google pin green.png" ofType:nil]];
}
else if([annotation.mPinColor isEqualToString:#"red"])
{
anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Google pin red.png" ofType:nil]];
}
else if([annotation.mPinColor isEqualToString:#"blue"])
{
anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Google pin blue.png" ofType:nil]];
}
annView.image = anImage;
return annView;
}
I thought this would be something that's fairly common - but cant find it anywhere.
I know how to put images on a map. I use CSMapAnnotation found here http://spitzkoff.com/craig/?p=81.
I've pretty much taken the CSMapAnnotationTypeImage as an example and made a CSMapAnnotationTypeLabel but it keep just showing pins on the map and not the UILabel like I expected.
the header file
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface CSLabelAnnotationView : MKAnnotationView {
UILabel* _label;
}
#property(retain, nonatomic) UILabel* label;
#end
and the source file
#import "CSLabelAnnotationView.h"
#import "CSMapAnnotation.h"
#import "util.h"
#implementation CSLabelAnnotationView
#synthesize label = _label;
#define kWidth 120
#define kHeight 20
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
self.frame = CGRectMake(0, 0, kWidth, kHeight);
self.backgroundColor = [UIColor clearColor];
CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
_label = [[UILabel alloc] initWithFrame:self.frame];
_label.text=csAnnotation.title;
[self addSubview:_label];
return self;
}
#if 0
- (void)prepareForReuse
{
TGLog(#"");
[_imageView removeFromSuperview];
[_imageView release];
}
#endif
-(void) dealloc
{
[_label release];
[super dealloc];
}
#end
to add it to the map
annotation = [[[CSMapAnnotation alloc] initWithCoordinate:coordinate
annotationType:CSMapAnnotationTypeLabel
title:i.name
reuseIdentifier:#"ocualabel"] autorelease];
[_mapView addAnnotation:annotation];
and
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView* annotationView = nil;
if (annotation == mapView.userLocation){
return nil; //default to blue dot
}
// determine the type of annotation, and produce the correct type of annotation view for it.
CSMapAnnotation* csAnnotation = (CSMapAnnotation*)annotation;
TGLog(#"csAnnotation.annotationType %d", csAnnotation.annotationType);
if(csAnnotation.annotationType == CSMapAnnotationTypeStart ||
csAnnotation.annotationType == CSMapAnnotationTypeEnd)
{
NSString* identifier = #"Pin";
MKPinAnnotationView* pin = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == pin)
{
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:csAnnotation reuseIdentifier:identifier] autorelease];
}
[pin setPinColor:(csAnnotation.annotationType == CSMapAnnotationTypeEnd) ? MKPinAnnotationColorRed : MKPinAnnotationColorGreen];
annotationView = pin;
TGLog(#"csPin anno");
}
else if(csAnnotation.annotationType == CSMapAnnotationTypeImage)
{
NSString* identifier = csAnnotation.reuseIdentifier;
CSImageAnnotationView* imageAnnotationView = (CSImageAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == imageAnnotationView)
{
imageAnnotationView = [[[CSImageAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
imageAnnotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
TGLog(#"CSImage anno");
annotationView = imageAnnotationView;
} else if(csAnnotation.annotationType == CSMapAnnotationTypeLabel)
{
NSString* identifier = csAnnotation.reuseIdentifier;
CSLabelAnnotationView* labelAnnotationView = (CSLabelAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == labelAnnotationView)
{
labelAnnotationView = [[[CSLabelAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
TGLog(#"CSLabel anno");
} else {
TGLog(#"%d", csAnnotation.annotationType);
}
[annotationView setEnabled:YES];
[annotationView setCanShowCallout:YES];
return annotationView;
}
Can anyone help me with this? Or is there some other standard way to get UILabels on a map?
Found it.
Needed to add this in viewForAnnotation():
annotationView = labelAnnotationView;