Hello i'm building a mapbased App when i'm tapping on the Accessory i perform a segue to a profile page which gives information about the pin location. Now i have a image in my LeftCalloutAccesoryView which a i want to show on the detail page. Now i'm curious if there is a method to get the image which a set on LeftCalloutAccesoryView. I add the images dynamically.
my accessorycontroltapped code:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
companytitle = view.annotation.title;
description = view.annotation.subtitle;
thumbprofileimage = view.image;
//thumbprofileimage = [(UIImageView *)view.leftCalloutAccessoryView setImage:pin.image];
//UIImageView *leftIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//leftIconView = [view.leftCalloutAccessoryView [[UIImageView alloc]];
//view.leftCalloutAccessoryView;
[self performSegueWithIdentifier:#"showPlattegrondDetails" sender:self];
}
my set pins code:
- (MKAnnotationView *)mapView:(MKMapView *)mpView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[RouteAnnotation class]]) {
MKPinAnnotationView *annotationView =
(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
leftIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[leftIconView setImage:pin.image];
annotationView.leftCalloutAccessoryView = leftIconView;
//annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
return nil;
}
Does someone have the solution of getting a reference to the image so i can send this image to the detail page?
You are already part way there with your mapView:annotationView:calloutAccessoryControlTapped: method.
Create a property to keep a reference to the image from the tapped callout:
#property (nonatomic, strong) UIImage *selectedCalloutImage;
And update the delegate method:
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
companytitle = view.annotation.title;
description = view.annotation.subtitle;
thumbprofileimage = view.image;
//thumbprofileimage = [(UIImageView *)view.leftCalloutAccessoryView setImage:pin.image];
//UIImageView *leftIconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//leftIconView = [view.leftCalloutAccessoryView [[UIImageView alloc]];
//view.leftCalloutAccessoryView;
// Keep a reference to the callout's image
self.selectedCalloutImage = [(UIImageView *)view.leftCalloutAccessoryView image];
[self performSegueWithIdentifier:#"showPlattegrondDetails" sender:self];
}
Then pass the image to the subsequent controller in the prepareForSegue:sender: method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showPlattegrondDetails"])
{
DetailViewController *controller = segue.destinationViewController;
controller.image = self.selectedCalloutImage;
}
}
You can then display / use it however you like.
Related
I am getting some image url from web server, which I need to display on mapview. I have added function for MKAnnotationView and parsed the url, however its not showing on map view. Below is my code. Please let me know, if I am doing anything wrong.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *identifier = #"CustomAnnotation";
MKAnnotationView* annView = nil;
if ([annotation isKindOfClass:[MapViewAnnotation class]]) {
annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annView == nil) {
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annView.annotation = annotation;
}
for(int i=0;i<array.count;i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *iconurl = [NSURL URLWithString:[[array objectAtIndex:i]valueForKey:#"icon"]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:iconurl]];
dispatch_async(dispatch_get_main_queue(), ^ {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[annView addSubview:imageView];
[annView setFrame:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
});
});
}
UIButton*accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[accessory addTarget:self action:#selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
[accessory setFrame:CGRectMake(0, 0, 30, 30)];
[annView setRightCalloutAccessoryView:accessory];
}
[annView setEnabled:YES];
[annView setCanShowCallout:YES];
return annView;
}
First, make sure you've specified the delegate of the map view and confirm that your viewForAnnotation is getting called at all.
Second, you're adding the UIImageView as a subview of the MKAnnotationView. Generally you just set the image property of the MKAnnotationView itself.
I used this one in the below delegates and set the image in annotation. When tapping the annotation its didselectannotation delegate is not called. But when I use the default then its working. Please suggest if anyone can help in this context.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
static NSString *defaultPinID = #"Annotation";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
UIImage *originalImage = [UIImage imageNamed:#"map.png"];
pinView.canShowCallout = YES;
pinView.image = [self drawImage:originalImage inRect:CGRectMake(0, 0, 106, 148)];
pinView.calloutOffset = CGPointMake(0, -5);
UIImageView *profileImageView = [[UIImageView alloc]init];
profileImageView.frame = CGRectMake(6, 7, 40, 40);
[profileImageView setImage:[UIImage imageNamed:#"user.png"]];
[profileImageView setBackgroundColor:[UIColor whiteColor]];
[self setRoundedAvatar:profileImageView toDiameter:40 atView:self.view];
[pinView addSubview:profileImageView];
I have done this using the gestureRecognizer to PinView.
UITapGestureRecognizer *tapLocation = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognizerShouldBeginFocusMap:)];
tapLocation.numberOfTapsRequired=1;
[pinView addGestureRecognizer:tapLocation];
- (BOOL)gestureRecognizerShouldBeginFocusMap:(UIGestureRecognizer *)gestureRecognizer
{
MKMapRect visibleMapRect = self.mapView.visibleMapRect;
NSSet *visibleAnnotations = [self.mapView annotationsInMapRect:visibleMapRect];
for ( MyAnnotation *annotation in visibleAnnotations ){
[self setCenterCoordinate:annotation.coordinate zoomLevel:18 animated:YES];
}
return YES;
}
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 a UIScrollView(gray) with a UIView (white) attached on. On the view I have attached an MKMapView with a pin inside.
For some unknown reason the pin doesn't show the popup when tapped.
Here some code:
UIView * whiteMapView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
whiteMapView.backgroundColor = [UIColor whiteColor];
whiteMapView.clipsToBounds = NO;
whiteMapView.userInteractionEnabled = YES;
[self.scrollView addSubview:whiteMapView];
x = 10.0f;
y = x;
w = CGRectGetWidth(whiteMapView.frame) - (x * 2);
h = w;
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(x, y, w, h)];
self.mapView.delegate = self;
self.mapView.scrollEnabled = NO;
[whiteMapView addSubview:self.mapView];
[self.mapView removeAnnotations:self.mapView.annotations];
Shop * shop = [[Shop alloc] init];
shop.name = self.userInfo[#"shopName"];
shop.shopDescription = self.userInfo[#"goods_description"];
shop.latitude = self.userInfo[#"lat"];
shop.longitude = self.userInfo[#"lon"];
MapPin * pin = [[MapPin alloc] initWithShop:shop];
[self.mapView addAnnotation:pin];
Here the delegate method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
if (annotation == mapView.userLocation)
{
return nil;
}
else
{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Pin"];
pinView.userInteractionEnabled = YES;
pinView.image = [UIImage imageNamed:#"pin_mappa_red"];
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
disclosureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
disclosureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[disclosureButton addTarget:self action:#selector(didPressPin:) forControlEvents:UIControlEventTouchUpInside];
disclosureButton.tag = [[[(MapPin *)annotation shop] idshop] intValue];
pinView.rightCalloutAccessoryView = disclosureButton;
return pinView;
}
}
In another part of the app this code works perfectly.
Ideas ?
Try to change like this viewForAnnotation as follows
-(MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if(annotation)
{
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImageView *imageInMap = [[UIImageView alloc]initWithImage: [UIImage imageNamed:#"net.png"]];
[disclosureButton addTarget:self action:#selector(mapCallOutPressed) forControlEvents:UIControlEventTouchUpInside];
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[self.locationMapVIew dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.rightCalloutAccessoryView = disclosureButton;
pinView.leftCalloutAccessoryView = imageInMap;
pinView.leftCalloutAccessoryView.frame = CGRectMake(0, 0, 35, 35);
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else {
[self.locationMapVIew.userLocation setTitle:#"I am here"];
}
return pinView;
}
Check this method is call or not
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog("Pin called");
}
If this method is not called then there may be any problem for mapview or Annotation.
I am trying to show callout on a pin by without clicking on the pin.In short, I want show the callout when the pins are getting placed on the map.
This is code I am using:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation // Method to handle all the annotations on map.
{
if([annotation isKindOfClass: [MKUserLocation class]])
return nil;
static NSString *annotationID = #"MyAnnotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;
if (!pinView) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"map_pin.png"];
//Setting Right call button
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Setting Left Call button
self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.favButton.frame = CGRectMake(0, 0, 23, 23);
self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.favButton setImage:[UIImage imageNamed:#"favourite.png"] forState:UIControlStateNormal];
pinView.leftCalloutAccessoryView = self.favButton;
[self.mapView selectAnnotation:annotation animated:YES];
}
return pinView;
}
Any help would be appreciable.
Try with This :
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
if ([currentAnnotation isEqual:annotationToSelect]) {
[mapView selectAnnotation:currentAnnotation animated:YES];
}
}
}
OR
EDIT :
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation // Method to handle all the annotations on map.
{
if([annotation isKindOfClass: [MKUserLocation class]])
return nil;
static NSString *annotationID = #"MyAnnotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;
if (!pinView) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"map_pin.png"];
//Setting Right call button
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Setting Left Call button
self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.favButton.frame = CGRectMake(0, 0, 23, 23);
self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.favButton setImage:[UIImage imageNamed:#"favourite.png"] forState:UIControlStateNormal];
pinView.leftCalloutAccessoryView = self.favButton;
**[self performSelector:#selector(selectAnnotation:) withObject:annotation afterDelay:0.5];**
}
return pinView;
}
- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
[self.mapView selectAnnotation:annotation animated:NO];
}