Below, I have created an Annotation for my map which works perfectly and shows up with the title and subtitle as it should.
But I wish to add a small image to the left of the annotation but can't figure out what I need to do to the below code to make it work.
// Annotation
CLLocationCoordinate2D poseLocation;
poseLocation.latitude = POSE_LATITUDE;
poseLocation.longitude = POSE_LONGITUDE;
Annotation *myAnnotation = [[Annotation alloc] init];
myAnnotation.coordinate = poseLocation;
myAnnotation.title = #"Pose Beauty Salon";
myAnnotation.subtitle = #"100, Moneyhaw Road";
[self.myMapView addAnnotation:myAnnotation];
You will need to set the delegate of your myMapView first and implement the viewForAnnotation delegate method.
There you will return MKAnnotationView instance which has a property leftCalloutAccessoryView:
The view to display on the left side of the standard callout bubble.
The default value of this property is nil. The left callout view is
typically used to display information about the annotation or to link
to custom information provided by your application. The height of your
view should be 32 pixels or less.
In the leftCalloutAccessoryView, you can assign your image there. For example:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:#"annotation"];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"annotation"];
UIImageView *imageView = //initialize your image view here
annotationView.leftCalloutAccessoryView = imageView;
}
annotationView.annotation = annotation;
return annotationView;
}
PS: Apparently you have asked similar question before here: Add image to the left of my annotations. I am not sure you need to post another question. Please try to implement this first.
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
myImage = [UIImage imageNamed:imagename];
CGRect cropRect = CGRectMake(0.0, 0.0,35.0, 35.0);
myImageView = [[UIImageView alloc] initWithFrame:cropRect];
myImageView.clipsToBounds = YES;
myImageView.image = myImage;
MyPin.leftCalloutAccessoryView =myImageView;
MyPin.highlighted=YES;
MyPin.canShowCallout=YES;
return MyPin;
}
It works for me , try this one
Related
I'm currently building an "Uber-like" application, not in the same business but same design, and I would like to know what should I do if I want to represent their "Set pickup location" View/Button.
I already saw this post : Customize MKAnnotation Callout View? which was about Custom Annotation Callout View
In older version it looked like a Custom Callout View, but now it's a bit different, and I don't know where to start if I want to have the same result :
Thanks for your help !
To set different image on left accessory and right accessory, use this code.
// set different pins colors
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *annotationView = nil;
if( [annotation isKindOfClass:[YOURANNOTATION class] ] )
{
static NSString * AnnotationID = #"YOURANNOTATION";
annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationID];
if( annotationView == nil )
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationID] ;
}
UIImage * flagImage = nil;
flagImage = [UIImage imageNamed:#"marker-map#1x.png"];
[annotationView setImage:flagImage];
annotationView.canShowCallout = YES;
// add an image to the callout window
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"marker-map#1x.png"]];
annotationView.leftCalloutAccessoryView = leftIconView;
//adding right button accessory
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = infoButton;
//image size and adding image on left accessory
CGRect resizeRect;
resizeRect.size = flagImage.size;
resizeRect.origin = (CGPoint){0.0f, 0.0f};
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
annotationView.image = resizedImage;
}
return annotationView;
}
Then to call selected annotation
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
YOURANNOTATION *annotation=(YOURANNOTATION*)view.annotation;
//do something with your annotation
}
I'm trying to add an image to the marker. How to achieve the best quality? As you can see the Google Map yourself increases it twice.
do you add an asset to you project or just one image?
So, you need to add asset of images (named like - bigMarker.png, bigMarker#2x.png, bigMarker#3x.png) to your project asset, like on screenshots. That works fine on iOS 9.
And that my sample of code in viewDidLoad:
// Add custom position in Kansas
CustomAnnotation *annotation = [CustomAnnotation new];
annotation.title = #"Elevation Burger";
annotation.subtitle = #"The best buns...";
annotation.coordinate = CLLocationCoordinate2DMake(39.0119020f,-98.4842460f);
[self.myMap addAnnotation:annotation];
And this one in delegate:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *annotationView = nil;
if (annotation != mapView.userLocation) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:nil];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:#"bigMarker"];
annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"burgerBitmap"]];
UIButton *accessoryButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImage *disclosureButtonImage = [[UIImage imageNamed:#"disclosure"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[accessoryButton setImage:disclosureButtonImage forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = accessoryButton;
}
return annotationView;
}
I am trying to change my pin colour to purple, when I do it I lose the title though. Code is:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBarHidden=YES;
//init the location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager requestWhenInUseAuthorization];
self.mapView.showsUserLocation=YES;
self.userGeoPoint=self.message[#"userLocation"];
self.pinView = [[MKPointAnnotation alloc] init];
self.pinView.title=self.message[#"fromUser"];
self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
self.pinView.coordinate=self.coord;
//use a slight delay for more dramtic zooming
[self performSelector:#selector(addPin) withObject:nil afterDelay:0.5];
}
-(void)addPin{
[self.mapView addAnnotation:self.pinView];
[self.mapView selectAnnotation:self.pinView animated:YES];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
return nil;
static NSString *annotationIdentifier = #"annotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
pinView.pinColor = MKPinAnnotationColorPurple;
//now we can throw an image in there
return pinView;
}
I tried setting the title property for MKPinAnnotation but there isn't one. Is there anyway I can get around this?
In viewForAnnotation, you need to set canShowCallout to YES (it's NO by default):
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
A couple of unrelated points:
It looks like you have a property named pinView of type MKPointAnnotation that you are using to create the annotation object in viewDidLoad. Then in viewForAnnotation, you have a local variable also named pinView of type MKPinAnnotationView. Although there is no naming conflict here, it causes and implies some conceptual confusion. MKPointAnnotation is an annotation model class while MKPinAnnotationView is an annotation view class -- they are completely different things. It would be better to name the annotation property pin or userAnnotation for example.
This comment in viewForAnnotation:
//now we can throw an image in there
seems to imply that you could set a custom image in the view at this point. Setting a custom image in a MKPinAnnotationView is not recommended since that class is designed to display default pin images in one of three colors. To use a custom image, create a plain MKAnnotationView instead.
The pinColor property was deprecated in iOS 9. From iOS 9 onwards you should use pinTintColor which takes a UIColor rather than a MKPinAnnotationColor.
Old:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinColor = MKPinAnnotationColorPurple;
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinTintColor = [UI Color purpleColor];
There is more information available in the Apple docs.
I just upgrade to ios 10, apple changed the api.
pinColor no longer in use, instead, use tintColor
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.tintColor = [UIColor purpleColor];
Note that the pin title will be displayed when the user taps it:
If the value of this property is true, a standard callout bubble is shown when the user taps a selected annotation view. The callout uses the title and subtitle text from the associated annotation object.
( https://developer.apple.com/documentation/mapkit/mkannotationview/1452451-canshowcallout )
I am trying to use a custom image on my MKAnnotationView when I use the following code I get no image on my annotation. I have checked in debug to ensure the image is being properly loaded into the UIImage.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"String"];
if(!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"String"];
UIButton *directionButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *directionIcon = [UIImage imageNamed:#"IconDirections"];
[directionButton setImage:directionIcon forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = directionButton;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
return annotationView;
}
There are two main issues:
The frame of the custom callout button is not set making it essentially invisible.
An MKAnnotationView is being created but its image property (the image of the annotation itself -- not the callout button's) is not set. This makes the whole annotation invisible.
For issue 1, set the button's frame to some appropriate value. For example:
UIImage *directionIcon = [UIImage imageNamed:#"IconDirections"];
directionButton.frame =
CGRectMake(0, 0, directionIcon.size.width, directionIcon.size.height);
For issue 2, set the annotation view's image (or create an MKPinAnnotationView instead):
annotationView.image = [UIImage imageNamed:#"SomeIcon"];
Additionally, you should handle view re-use correctly by updating the annotation property.
Complete example:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"String"];
if(!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"String"];
annotationView.image = [UIImage imageNamed:#"SomeIcon"];
UIButton *directionButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *directionIcon = [UIImage imageNamed:#"IconDirections"];
directionButton.frame =
CGRectMake(0, 0, directionIcon.size.width, directionIcon.size.height);
[directionButton setImage:directionIcon forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = directionButton;
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
}
else {
//update annotation to current if re-using a view
annotationView.annotation = annotation;
}
return annotationView;
}
In order for the callout to be shown, the annotation must be selected. To do this programmatically, call:
[mapView selectAnnotation:annotation animated:YES];
where annotation is the specific MKAnnotation for which you want a callout displayed.
You'll almost certainly want to put this in - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views.
There are a few caveats to consider, so here are two other posts that have some great answers and relevant discussions:
How to trigger MKAnnotationView's callout view without touching the pin?
Wanted: How to reliably, consistently select an MKMapView annotation
I have a MapView with two pins on it (custom pins).
Both pins are set to draggable, but my problem is before I can drag one of them I first have to select it before I can start dragging it. This means two taps on the screen.
I know about this answer, but he only has one pin ons his map and it seems to me only one pin at a time can be selected so setting the [MyPin setSelected:YES]; wouldn't help me in this case.
Thanks for the help!
//Custom pin on mapview
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *MyPin=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.draggable = YES;
//Get annotaion title to determine what image to use
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint = annotation;
if([annotationPoint.title isEqualToString:#"user"])
{
MyPin.image = [UIImage imageNamed:#"userLocation_pin"];
MyPin.centerOffset = CGPointMake(-13, -5); //Offset custom image to display at the exact pin point GPointMake([left/right], [up/down]);
}
else if ([annotationPoint.title isEqualToString:#"destination"])
{
MyPin.image = [UIImage imageNamed:#"destination_pin_up"];
MyPin.centerOffset = CGPointMake(-13, -5); //Offset custom image to display at the exact pin point GPointMake([left/right], [up/down]);
}
return MyPin;
}
Managed to solve my own problem by adding the [MyPin setSelected:YES]; inside the if statements like this:
//Custom pin on mapview
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *MyPin=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.draggable = YES;
//Get annotaion title to determine what image to use
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint = annotation;
if([annotationPoint.title isEqualToString:#"user"])
{
MyPin.image = [UIImage imageNamed:#"userLocation_pin"];
MyPin.centerOffset = CGPointMake(-13, -5); //Offset custom image to display at the exact pin point GPointMake([left/right], [up/down]);
[MyPin setSelected:YES];
}
else if ([annotationPoint.title isEqualToString:#"destination"])
{
MyPin.image = [UIImage imageNamed:#"destination_pin_up"];
MyPin.centerOffset = CGPointMake(-13, -5); //Offset custom image to display at the exact pin point GPointMake([left/right], [up/down]);
[MyPin setSelected:YES];
}
return MyPin;
}