How can i make custom pin and use FollowWithHeading mode together? - ios

The following code below is show custom pin (picture as pin). it can use normally.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
PVAttractionAnnotationView *annotationView = [[PVAttractionAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Attraction"];
annotationView.canShowCallout = YES;
return annotationView;
}
Then use following code to show current location
[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading];
XCODE jump to main.m and show
Thread 1:Signal SIGABRT
On the other hand if i use the following code
[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading];
and unused all of the following code
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
PVAttractionAnnotationView *annotationView = [[PVAttractionAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Attraction"];
annotationView.canShowCallout = YES;
return annotationView;
}
Application will show current location normally but it's not show custom pin. It's show the red pin that is the default of system cause i've unused that code.
How can i make custom pin and use FollowWithHeading mode together?
..I'm sorry I do not use English well.

You need a slight change to your viewForAnnotation that examines the class of the annotation and returns the appropriate view. By returning nil the system will use the default view. You also need some additional code to implement view re-use correctly -
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView=nil;
if ([annotation isKindOfClass:[PVAttractionAnnotation class]]) // Note - put your custom annotation class here
{
annotationView =(MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"Attraction"];
if (annotationView == nil)
{
annotationView = [[PVAttractionAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"Attraction"];
annotationView.canShowCallout = YES;
}
else
{
annotationView.annotation=annotation;
}
}
return annotationView;
}

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:].

How to disable Accessibility for MKAnnotationView and Street in MKMapView

I want to disable some pins on a map view. Setting isAccessibilityElement = NO is not working on MKAnnotationView somehow.
In addition, I found VoiceOver would iterate from street to street in the map view which may not make sense in some scenarios.
So is there other way to disable the Accessibility on MKAnnotationView and the street in the map?
Current code
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) return nil;
...//some setup
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!annotationView && ![annotationIdentifier isEqualToString:#""]) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
}
.../some config
// somehow no effect :(
annotationView.isAccessibilityElement = NO;
return annotationView;
}

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.

Custom icon for location.Possible?

After much googling I'm nothing has come to my specific problem.
Is it possible to change the icon of the blue location dot with the circle?
I would like to replace the locator icon with my own icon.
Then save it to a different view with my own image.By pressing a button.Like a Annotation.
The result would be the location in the "AddPlace"View should store the location in a different View.
I use the MapKit Framework.
Yes, there is delegate method of MKMapView - viewForAnnotation, which is called also for the location pin. When annotation is kind of class MKUserLocation
Example:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
MKAnnotationView *pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pinViewLocation"];
if (!pinView) {
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pinViewLocation"];
pinView.canShowCallout = NO;
pinView.image = [UIImage imageNamed:#"map-location-pin"];
}
}
.....

setting the MKMapViewDelegate makes my marker disappear in iOS

I am using MapKit and I am having the exact problem.
This is my code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [mymap_ios dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
} else {
annotationView.annotation = annotation;
}
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
return nil;
}
In this code, I can see the pin but not the blue button next to it so as. It seems that I have forgotten to do this:
mymap_ios.delegate=self;
But when I add this, the marker is not shown at all.
Can you help me on that?
When you don't set the map view's delegate, it doesn't call your viewForAnnotation and creates a default red pin without any accessory buttons.
When you set the delegate, it is calling your viewForAnnotation method but you are creating a plain MKAnnotationView which by default does not have any pre-set image or view (it's blank).
Either set the annotation view's image, add some content to the view, or simply create an MKPinAnnotationView instead of an MKAnnotationView:
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mymap_ios ...
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] init...
Also make sure that the annotation objects you add are of type MyLocation otherwise they will appear as plain red pins without an accessory button.

Resources