I have a MapView with cluster annotations (ADMapCluster). I want to show the amount of elements in the Cluster, therefor I'm adding a UILabel to the MKAnnotationView as a Subview.
My Problem is that when I reuse the MKAnnotationView after zooming or
similar actions the UILabel doesn't update the text.
- (MKAnnotationView *)mapView:(ADClusterMapView *)mapView viewForClusterAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKAnnotationView * pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"ADMapCluster"];
if (!pinView) {
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"ADMapCluster"];
pinView.image = [UIImage imageNamed:#"map-markers"];
UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 5, 25, 20)];
countLabel.textAlignment = NSTextAlignmentCenter;
[countLabel setTag:2];
countLabel.text = [NSString stringWithFormat:#"%d",[[(ADClusterAnnotation*)pinView.annotation originalAnnotations] count] ];
countLabel.font = [UIFont fontWithName:#"Avenir-Book" size:10];
[pinView addSubview:countLabel];
}
else {
pinView.annotation = annotation;
[((UILabel*)[pinView viewWithTag:2]) setText:[NSString stringWithFormat:#"%d",[[(ADClusterAnnotation*)pinView.annotation originalAnnotations] count] ]];
[((UILabel*)[pinView viewWithTag:2]) setNeedsDisplay];
}
return pinView;
}
Any idea what I'm doing wrong and why the labels don't get updated?
Related
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 multiple Annotation in a MapView, listed in 3 different arrays.
I've used the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation method to change te Callout of the annotation.
The weird thing is that my UserLocation is changing into an Customised Annotation.
Why is that, and what could be the problem?
How I listed my Annotations:
myAnn = [[Annotations alloc]init];
location.latitude = 52.338847;
location.longitude = 4.937482;
myAnn.coordinate = location;
myAnn.title = #"Afvalpunt";
myAnn.subtitle = #"Rozenburglaan 1";
[category3 addObject:myAnn];
[self.locationArrays addObject:category3];
self.currentAnnotation = 0;
[self.myMapView addAnnotations:[self.locationArrays objectAtIndex:0]];
How my Method is set up:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"MapAn"];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MapAn"];
annotationView.canShowCallout = YES;
//Blauw Navigatie Auto...
UIImageView *carView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Driving"]];
UIButton *blueView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44+30)];
blueView.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
carView.frame = CGRectMake(11, 14, carView.image.size.width, carView.image.size.height);
[blueView addTarget:self action:#selector(carClicked) forControlEvents:UIControlEventTouchUpInside];
[blueView addSubview:carView];
annotationView.leftCalloutAccessoryView = blueView;
}
return annotationView;
}
Try this code.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if (annotation == mapView.userLocation) return nil;
...
We return nil if the annotation is userLocation to let the mapView display the blue dot & circle animation. In order to show our custom annotation for userLocation just remove the line return nil; and do your customization there.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* AnnotationIdentifier = #"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
if (annotation == mapView.userLocation)
{
customPinView.image = [UIImage imageNamed:#"myCarImage.png"];
}
else
{
customPinView.image = [UIImage imageNamed:#"mySomeOtherImage.png"];
customPinView.animatesDrop = NO;
customPinView.canShowCallout = YES;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
Hope this code is useful for you.
Try this code
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]] || annotation==mapView.userLocation)
{
return nil;//Here you can also customise your pin if you dont want pin then just return nil.
}
else
{
//your code
}
}
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];
}