I am developing and app in Xcode (objective-c) and I have a problem. I have set a leftCalloutAccessoryView image in my map pins but my problem is that the image size is too big. If it is possible, I want to resize the UUImage without changing the real size of the image, because if I change the size of the image, the image gets worst quality and I can see it pixeled.
This is my viewForAnnotation methof whre I have implemented my leftCalloutAccessoryView:
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"pin2#2x.png"];
}
else {
//[mapView.userLocation setTitle:#"I am here"];
}
/**Pone una imagen a cada map pin leftimage**/
UIImageView *leftIconView;
leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"80_grados.png"]];
pinView.leftCalloutAccessoryView = leftIconView;
leftIconView.layer.cornerRadius = 6;
leftIconView.clipsToBounds = YES;
leftIconView.layer.borderWidth = 1;
/********/
UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = pinButton;
return pinView;
}
This is what I want to get without changing the image size because it reduces their quality:
Do you know how can I size my leftIconView?
Thank you very much for your response.
I used sample image to fix your issue .Please use the below code to fit the image into pinview
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Bartender"]];
leftIconView.contentMode = UIViewContentModeScaleAspectFit;
leftIconView.frame = CGRectMake(5, 5, pinView.frame.size.height- 20, pinView.frame.size.height - 20);
pinView.leftCalloutAccessoryView = leftIconView;
leftIconView.layer.cornerRadius = 5;
leftIconView.clipsToBounds = YES;
And also please find the attached screenshot for reference
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 dynamically change the callout image based on json response.But only one image display for all callouts please help me how to do this
#pragma mark Map View Delegate Methods
method for view for annotation
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )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.
MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomPinAnnotationView"];
if (!pinView)
{
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"pin.png"];
pinView.frame = CGRectMake(0, 0, 25, 30);
pinView.calloutOffset = CGPointMake(0, 32);
// Add a detail disclosure button to the callout.
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
NSLog(#"JSON: %#", islandArray); // print
//to display a island image in callout view
UIImageView *calloutImage = [[UIImageView alloc] init];
NSLog(#"%#",[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:#"mapImage"]]);
calloutImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:#"mapImage"]]]];
calloutImage.frame = CGRectMake(10, 0, 30, 30);
[calloutImage.layer setCornerRadius:05.0f];
pinView.leftCalloutAccessoryView = calloutImage;
pinView.tintColor = MKPinAnnotationColorRed;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
I have changed my MapView pin image but I am getting this problem that some of the point wont change the pin image and some of them change. Where would be the problem? I have added an example.
My Code:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
MKAnnotationView *pinView = nil;
if(annotation != locationMap.userLocation)
{
static NSString *defaultPinID = #"myPin";
pinAnnotation = (MKPinAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.canShowCallout = YES;
pinAnnotation.animatesDrop = YES;
pinAnnotation.enabled = YES;
//pinAnnotation.image = [UIImage imageNamed:#"pin.png"];
pinView.image = [UIImage imageNamed:#"pin.png"];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
return pinAnnotation;
return pinView;
}
Use a regular MKAnnotationView, not the MKPinAnnotationView subclass. Even though you can set the image it isn't guaranteed to stick because it can and will set the pin image back again.
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