I am working on maps and when you tap a annotation it shows the title and description.
Problem is one annotation is very close to the user's current location so i can't see the title of that annotation because its not tapeable. When i try to tap the annotation nothing happens.
- (MKAnnotationView *) mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
//Don't trample the user location annotation (pulsing blue dot).
return nil;
}
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pin"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
if (pinView == nil)
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pin"];
if([[pinView.annotation title] isEqualToString:#"Store"])
{
pinView.pinColor = MKPinAnnotationColorGreen;
}
return pinView;
}
Is there anyway i can bring the annotation front that blue dot that shows current location?
So i moved
pinView.canShowCallout = YES;
in the last before returning the pinView and it worked.
Related
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;
}
I created an if-then statement to show a purple pin if the title of the pin is "HQ". The green pins show up correctly. Does anyone know why my pin color for the purple pin is still red?
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MyAnnotation class]])
{
static NSString *annotationIdentifier=#"annotationIdentifier";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(annotationView)
{
annotationView.annotation = annotation;
}
else
{
annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
if([[annotationView.annotation title] isEqualToString:#"HQ"])
{
//The pin for the HQ should be purple
annotationView.pinColor = MKPinAnnotationColorPurple;
[annotationView setAnimatesDrop:YES];
}
else
{
//All other new pins should be "green" by default
annotationView.pinColor = MKPinAnnotationColorGreen;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[annotationView setAnimatesDrop:YES];
}
}
return annotationView;
}
return nil;
}
the map reuses annotation view (or offers to do so)
same as a tableView does cell reusing.
now upon init you set the pin color, but on reusing you don't. so one pin is always the same color.
use different reuse identifiers for the different cases!
static NSString *annotationIdentifier= ([annotation.title isEqualToString:#"HQ"]) ? #"HQ" : "OTHER";
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.
I was wonderring if there is a way to change those red pins that are used as markers. And if there is a way, how to do it?
you can use 3 types of color pins in mapView are bellow..
MKPinAnnotationColorGreen;
MKPinAnnotationColorPurple
MKPinAnnotationColorRed
and if you want to add customview or image then you can add with programatically
also you can change pin in delegate method of MKMapView like bellow..
- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
if (annotation == _mapView.userLocation)
{
// pinView.pinColor = MKPinAnnotationColorRed;
// return pinView;
return nil;
}
pinView.pinColor = MKPinAnnotationColorGreen; // you can use MKPinAnnotationColorPurple, MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
return pinView;
}
and for custom image or pin, use bellow code..
- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.image = [UIImage imageNamed:#"yourImageName"];//add any image which you want to show on map instead of red pins
annotationView.annotation = annotation;
return annotationView;
}
I dont understand why the userLocation doesnt move when i set a costum image on it.
If i don't set an image, it uses the default pin blue and it works; i can see the userlocation moves when the postion changes.
here is my viewForAnnotation taken from an other post.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString* AnnotationIdentifier = #"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mk dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
}
else
{
pinView.annotation = annotation;
}
if (annotation == mk.userLocation)
pinView.image = [UIImage imageNamed:#"PositionPin.png"];
else
pinView.image = [UIImage imageNamed:#"PositionPin.png"];
return pinView;
}
This appears to be a bug in the map view (still present in iOS 6) where the user location coordinates no longer update when a custom annotation view is used (even though documentation suggests it will work).
A workaround is to use Core Location to get user location updates and create your own annotation (instead of using the map view's userLocation) and then supplying a custom image for it in viewForAnnotation.
See the answer to Custom Annotation view for userlocation not moving the mapview for more details and some sample code to implement the workaround.
In addition, note that when using a custom image for your annotation view, you should use MKAnnotationView instead of MKPinAnnotationView.
When using MKPinAnnotationView, it sometimes shows its default pin image instead of your custom image.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString* AnnotationIdentifier = #"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mk dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop = NO;
pinView.canShowCallout = YES;
}
pinView.annotation = annotation;
pinView.image = [UIImage imageNamed:#"PositionPin.png"];
return pinView;
}