ios: Dont have current location become a pin on map - ios

I have pins that drop on a map and the current location is shown with a blue dot. I added:
- (MKAnnotationView *)mapView:(MKMapView *)amapView viewForAnnotation:(id<MKAnnotation>)annotation{
NSString *identifier =#"mypin";
MKPinAnnotationView *pin = (MKPinAnnotationView *) [amapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(pin ==nil){
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}else{
pin.annotation = annotation;
}
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[myDetailButton addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
pin.rightCalloutAccessoryView = myDetailButton;
pin.enabled = YES;
pin.animatesDrop = TRUE;
pin.canShowCallout = YES;
return pin;
}
But now the current location is a pin instead of the blue dot. How can I stop the annotation for the current location from becoming a pin and keep it as the blue dot.
Any help please?

The "blue dot" is a special annotation, a MKUserLocation. So, in your viewForAnnotation, just add the following two lines at the start to tell iOS to use the standard "blue dot" for the user location annotation:
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
By returning nil, you will tell iOS to use the default "blue dot" annotation for the user location.
For an example of this in practice, see the code sample in Creating Annotation Views from Your Delegate Object section of the Location Awareness Programming Guide.

Simple :
- (MKAnnotationView *)mapView :(MKMapView *)maapView viewForAnnotation:(id <MKAnnotation>) annotation{
#autoreleasepool {
if (annotation == maapView.userLocation)
{
// This code will execute when the current location is called.
return nil;
}
else
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
annView.pinColor = MKPinAnnotationColorPurple;
annView.animatesDrop=YES;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annView.rightCalloutAccessoryView = rightButton;
return annView;
}
}
}

Clean and Swifty (3) way
if annotation is MKUserLocation {
return nil
}

Related

Annotation Right Accessory Button won't change

My annotations trigger the right actions, but they aren't showing the right accessory button no matter which button I choose. Below is my code.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *s = #"ann";
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:s];
if (annotation != self.mapView.userLocation) {
pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:s];
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.rightCalloutAccessoryView = button;
[button addTarget:self
action:#selector(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
}
return pin;
}
Try this:
- (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.
MKAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:#"ann"];
if(pin){
pin.annotation = annotation;
} else {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"ann"];
}
pin.canShowCallout = YES;
pin.calloutOffset = CGPointMake(0, 0);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.rightCalloutAccessoryView = button;
[button addTarget:self
action:#selector(getDirections:)
forControlEvents:UIControlEventTouchUpInside];
return pin;
}

Button title doesn't show with annotation

Here's my problem. I created a red pin with a button thanks to my viewForAnnotation method.But the button doesn't show. Here my code:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//static NSString *defaultPinID = #"identifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:#"identifier"];
if ( pinView == nil )
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"identifier"];
pinView.enabled = YES;
pinView.pinColor=MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoDark];
[btn setTitle:#"test" forState:UIControlStateNormal];
pinView.rightCalloutAccessoryView = btn;
}
else
{
pinView.annotation = annotation;
}
pinView.annotation = annotation;
return pinView;
}
Can someone help me plz?
Have you added the annotation to you map?
Have you set the MKMapView's delegate?
Does your viewForAnnotation function get called?

How can i change mapkit annotation detail view background color?

Hi am trying to change the annotation detail view(show in map image) into a custom design like the second image.
How can i change that background image and all?
my current code is
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
mapView.showsUserLocation = YES;
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *identifier = #"myAnnotation";
MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[_MAPVIEW dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.pinColor = MKPinAnnotationColorPurple;
annotationView.canShowCallout = YES;
}else {
annotationView.annotation = annotation;
}
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(showDetailView) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = infoButton;
UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"pin.png"]];
annotationView.leftCalloutAccessoryView=imageV;
return annotationView;
}
current annotation detail view design(when i click on pin then i can see the annotation detail view )
but i want something like this design
please help me.

standard map annotation for iOS

I want to display my custom annotations in the map and my current location as the standard pin in the map view with the blue color.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MapPin";
if ([annotation isKindOfClass:[MyAnnotations class]]) {
MyAnnotations *ann= annotation;
MKAnnotationView *annotationView = (MKAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
if (ann.custom){
annotationView.image = [UIImage imageNamed:#"custom.png"];
}else{
//?? annotationView.image = [UIImage imageNamed:#"bluePin.png?"];
}
} else {
annotationView.annotation = annotation;
}
if(ann.custom){
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[nextButton addTarget:self action:#selector(annotationPicked) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=nextButton;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.multipleTouchEnabled = NO;
return annotationView;
}
return nil;
}
If the custom property is to distinguish between your annotations and the map view's user location, that case is already handled by the first if which checks the annotation's class and the custom property would be unnecessary.
The map view's user location annotation is of type MKUserLocation so the code will return nil in that case and the map view will show the standard blue dot (assuming showsUserLocation is YES).
However, if the custom property is to distinguish between two types of your own annotations, then one problem is that it doesn't handled re-used annotation views properly (when annotationView != nil).
When an annotation view is re-used, its image and rightCalloutAccessoryView may not be right for the current annotation so those properties need to be set (or cleared) whether a view is being re-used or not. For example:
if ([annotation isKindOfClass:[MyAnnotations class]]) {
MyAnnotations *ann= annotation;
MKAnnotationView *annotationView = (MKAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.multipleTouchEnabled = NO;
} else {
annotationView.annotation = annotation;
}
//set view properties that depend on annotation-specific properties
//regardless of whether view is new or re-used...
if (ann.custom)
{
annotationView.image = [UIImage imageNamed:#"custom.png"];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[nextButton addTarget:self action:#selector(annotationPicked) forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView=nextButton;
}
else
{
annotationView.image = [UIImage imageNamed:#"bluePin.png"];
annotationView.rightCalloutAccessoryView = nil;
}
return annotationView;
}
Though if the custom property is just to separate all your annotations from the map view's user location, it's unnecessary in the first place (as long as you check the annotation's class and set showsUserLocation to YES).
may be you put custom code in this event .because when you click on current location annotation this method calls and may be bacause of that code application crashes.
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
}
Thanks.

callout is not showing by default in map

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

Resources