How to zoom in on annotation click WITHOUT open the annotationView? - ios

I'm using the Apple maps. At the moment I have to click on one of my Annotation Pins and the Annotation View is opening, than I can click on this view and something happens. But i want to click on my Annotation Pins and the Map should be zoom in WITHOUT opening the Annotation View first. I tried this, but it doesn't work:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if(someValues > 1){
//If someValues are bigger than one then only zoom in without returning the annotation View
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
span.latitudeDelta = region.span.latitudeDelta/5;
span.longitudeDelta = region.span.longitudeDelta/5;
region.span = span;
region.center = anntotation.coordinate;
[mapView setRegion:region animated:TRUE];
//return 0 returns a default view i know....whats correct?
return 0;
} else {
MKPinAnnotationView *view = nil;
if (annotation != mapView.userLocation)
{
view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"identifier"];
if (nil == view) {
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"identifier"];
}
[view setPinColor:MKPinAnnotationColorRed];
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[view setCanShowCallout:YES];
[view setAnimatesDrop:NO];
}
return view;
}
}
Is there any delegate Method i´m missing?

please see the didSelectAnnotationView delegate method to your mapview.
example :
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if([[view annotation] isKindOfClass:[myMarker class]]) return;
}
And here :
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if([[view annotation] isKindOfClass:[myMarker class]]) return nil;
}

Related

How to change UILabel value in CustomAnnotation by clicking button in iOS

I have added a custom annotation and a percentage label on it.
By pressing the button in red circle, I want to change value of label from percentage to business name.
My Code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[BusinessCustomAnnotation class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [mapViewOffers dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
UILabel* category = [[UILabel alloc] initWithFrame:CGRectMake(annotationView.frame.size.width / 2, 15, 55, 20)];
BusinessCustomAnnotation *myAnnotationView = (BusinessCustomAnnotation *)annotation;
NSLog(#"Type One Offer! = %i", mapTypes);
[category setAdjustsFontSizeToFitWidth:YES];
if (mapTypes == 1) {
category.text = [NSString stringWithFormat:#"%#%#", myAnnotationView.offerPercentage, #"%"];
}else if (mapTypes == 2){
category.text = myAnnotationView.businessName;
}else if (mapTypes == 3){
category.text = myAnnotationView.businessName;
}
[category setMinimumScaleFactor:1.0];
category.font = [UIFont systemFontOfSize:15.0 weight:5.0];
category.textAlignment = NSTextAlignmentCenter;
[annotationView addSubview:category];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
annotationView.image = [UIImage imageNamed:#"iconMapMarker"];//here we use a nice image instead of the default pins
} else {
annotationView.annotation = annotation;
}
return annotationView; }return nil; }
Above mapview delegate is calling for one time only.
Waiting for the solution.
Thanks in advance for helping me.
There are two ways of detecting user interaction with your annotation view. The common technique is to define a callout (that standard little popover bubble that you see when you tap on a pin in a typical maps app) for your MKAnnotationView. And you create the annotation view for your annotation in the standard viewForAnnotation method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"loc"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
By doing this, you get a callout, but you're adding an right accessory, which is, in my example above, a disclosure indicator. That way, they tap on your annotation view (in my example above, a pin on the map), they see the callout, and when they tap on that callout's right accessory (the little disclosure indicator in this example), your calloutAccessoryControlTapped is called.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//first check your view class here
// here your code for change text on view
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//first check your view class here
// here your code for change text on view
}
You need to refresh the annotations. In Action of button try this :
mapTypes = 2
for (id<MKAnnotation> annotation in mapView.annotations)
{
[mapView removeAnnotation:annotation];
[mapView addAnnotation:annotation];
}
You can't make any changes to already added annotation pin.To make any changes to the annotation pin you need to remove all the pin and add it back.
Annotations don't refresh.
You have to remove all existing annotations with
[self.mapView removeAnnotations:self.mapView.annotations];
and update your "mapTypes" variable value to "2" or "3" in order to show business name.
Then you can can add your annotations again with [MKMapView addAnnotation:].

Change annotation pin color on MKMapView

I am trying to change colour of annotation pin on MKMapView by overriding this below:
- (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]])
{
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"CustomPinAnnotationView"];
if (!pinView)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorGreen;
} else {
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
Here below are relevant methods:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
// display map ????
PFGeoPoint *geoPoint = self.detailItem[#"location"];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(geoPoint.latitude,geoPoint.longitude);
MKPointAnnotation *annotation =[[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
// remove previous annotation pin
if([self.mapView.annotations count] == 1) {
[self.mapView removeAnnotation:[self.mapView.annotations objectAtIndex:0]];
}
[self.mapView addAnnotation:annotation];
annotation.title = self.detailItem[#"text"];
MKCoordinateSpan span = {.latitudeDelta = 0.05, .longitudeDelta = 0.05};
MKCoordinateRegion region = {coordinate, span};
[self.mapView setRegion:region];
[self.mapView setCenterCoordinate:self.mapView.region.center animated:NO];
}
}
My problem is pin annotation colour is still shown Red as default not Green as I want. What am I missing here?
Credit to #Anna, add the line below to - (void)configureView
self.mapView.delegate = self;

User location is showing like annotation

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
}
}

performing segue when a map annotation pin is tapped

I'm trying to perform a segue when a map annotation pin is tapped. Im using a custom annotation class if that makes a difference.
I've tried
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(#"annotation selected");
[self performSegueWithIdentifier:#"mySegue" sender:self];
}
but my NSLog isnt running so I assume the method isnt getting called.
The only other things I've done are add the annotation pin to my map view and set my view controller as the map view delegate.
Here is how I added the annotation to the mapview
SPMapAnnotation *pin = [[SPMapAnnotation alloc] init];
pin.coordinate = spotLocation.coordinate;
pin.title = [spot objectForKey:#"spotName"];
[self.mapView addAnnotation:pin];
How can I make this work? That didSelectAnnotation method seems like it would make this easy to do but I'm not sure how it works.
Apparently I forgot I didnt allocate my mapView until viewDidAppear. Just needed to add
self.mapView.delegate = self;
after I alloc and inited it.
Implement following delegate method
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = nil;
if ([annotation isKindOfClass:[SPMapAnnotation class]])
{
annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:#"Pin"];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Pin"];
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
}
}
return annotationView;
}
as didSelectAnnotationView will not be called for standard annotation pin, you need to return MKAnnotationView for didSelectAnnotationView to be called.

how to represent a constantly moving signal on a MapView?

Im just getting into MapViews on iOS, and want to show a car continuously moving as a blue dot. Would this be considered a map annotation?
Yes. As an example check out the in Simulator Debug > Location > City Bike Ride . It does a slow loop round San Francisco(?)
To listen to updates implement in your Mapview delegate
- (void)mapView:(MKMapView *)amapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"im here! - %f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}
and to adjust the annotation implement
- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id <MKAnnotation>) annotation{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
NSLog(#"annotation = %#",((NSObject *)annotation));
MKAnnotationView *annView;
annView = [amapView dequeueReusableAnnotationViewWithIdentifier:#"currentloc"];
if(!annView)
{
annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"] autorelease];
((MKPinAnnotationView *)annView).pinColor = MKPinAnnotationColorGreen;
((MKPinAnnotationView *)annView).animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
annView.draggable = YES;
}
return annView;
}
The snip I have put in just goes with the default blue dot with accuracy circle by returning nil for MKUserLocation but your implementation may be different.

Resources