Remove MKMapView Overlay on Button Push - iphone

I have a MKMapView with a MKOverlay over it showing the location history of the user. On a button press, how can I discard this overlay and remove it from view?
I have tried [map removeOverlay:overlay]; but that doesn't work - it still shows.

This will work
NSArray *pointsArray = [mapView overlays];
[mapView removeOverlays:pointsArray];

Just to add that, for my iPad application, I needed to add an extra line to the solution shown above:
NSArray *pointsArray = [self.mapView overlays];
[self.mapView removeOverlays:pointsArray];
self.mapOverlayView = nil;
Without setting mapOverlayView to nil, the "removeOverlays" call didn't seem to do much (?)

Related

SetUserTrackingMode not working

I am programming a map app on iPhone and want the map to rotate as the user changes his direction. I have read most of the posts on stackoverflow. Most of them suggest the use of setUserTrackingMode with MKUserTrackingModeFollowWithHeading if we are working with iOS 5 or later. This does not seem to work with me for some reason. Following is my code:
-(IBAction)getLocation //This is a button
{
mapView.showsUserLocation=YES; //mapView is the instance of MKMapView
[mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
}
This only shows the user location but if I move the phone, it doesn't rotate.
One more thing is, I downloaded a project from internet, and I included this line. It worked there only for the first time. I have no idea why this is happening.
Any suggestions?
You need to wait for the 'MapView' finish loading...
follow:
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
mapView.userTrackingMode = MKUserTrackingModeFollow;
}
follow & heading:
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
}
The easier way to do this is to include an MKUserTrackingBarButtonItem instead of creating your own button. It acts exactly the same as the button in the iOS 5 Maps app and is easy to set up.
Here's how to use it:
// You should have an outlet to your map view called mapView
MKUserTrackingBarButtonItem *userTrackingButton;
userTrackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView];
// You need an outlet to your toolbar too
[self.toolbar setItems:[NSArray arrayWithObject:userTrackingButton]];
Change "Animated" to "animated" and try again

iOS rotating MKAnnotationView in response of MKMapView rotation

In my application I have a MKMapView where several annotations are shown. The map rotates based on the heading of the device. To rotate the map the following statement is performed (called by the method locationManager: didUpdateHeading:)
self.navigationMapView.mapView.transform = CGAffineTransformMakeRotation(-heading);
where the heading (magnetic) is expressed in radians. What I noticed it's that even the annotations in the map rotate and I don't want it. I tried to fix it in the following method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
static NSString *identifier = #"AnnotationViewIdentifier";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (av == nil) {
av = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
}
else{
av.annotation = annotation;
}
av.transform = CGAffineTransformMakeRotation(degreesToRadians(self.arController.currentHeading.magneticHeading));
av.canShowCallout = YES;
return av;
}
and I want to call this method from "didUpdateHeading:" but I really don't know how to do it. The TableView class has the reloadData function that calls the delegate method but here the things seem different. Any suggestions?!
Another question, my annotations on the map show the distance from the user, I would like to update them (distance label) as soon as the user change location. Any suggestions?!
So with a MKMapView having that be called properly is a little bit annoying. Essentially you have one of two options. Option 1: Create an array of the annotation on the screen and remove that from the map_view and then re-add them to the map_view. Essentially creating your own reload data function. Option 2: Do something simple such as
CGLocationCoordinate2D coordinate = map_view.center;
map_view.center = coordinate;
-- Essentially the point is to reset a property of the map causing it to redraw. However this option is not always going to work. Option 1 has a higher chance of working however that one can also fail, so if simply taking the annotations off and re-adding them causes nothing to happen then simply decreate the map and then recreate the map at the end of your map refresh function something like.
[my_map_view removeFromSuperView];
[my_map_view release];
my_map_view = nil;
my_map_view = [[MKMapView alloc] initWithFrame:CGRectMake(0,0,320,480)];
one of these options should work. I had to do option one for my solution however I know some people are lucky and option 2 works just as well.

MKMapview annotations go wrong after zooming in and zooming out

I'm using MKMapView with many annotations. Everything is OK until i zoom in and zoom out my map. Some annotations' location switch with each others. Anyone know why?
Here are my code that i call after viewDidLoad to import annotations from 3 arrays: longitudes, latitudes and photoFileName.
photoFileName contains photo file names for all annotations, longitudes and latitudes contain coordinates of them.
for (int i=0; i<[longitudes count]; i++)
CLLocation* location = [[CLLocation alloc] initWithLatitude:[latitudes objectAtIndex:i]
longitude:[longitudes objectAtIndex:i]];
CSMapAnnotation* annotation = [[CSMapAnnotation alloc] initWithCoordinate:[location coordinate]
annotationType:CSMapAnnotationTypeImage
title:#"";
subtitle:#"";
// Set data for the annotation. This data is used for displaying annotation
[annotation setUserData:[photoFilename objectAtIndex:i]];
[_mapView addAnnotation:annotation];
[annotation release];
[currentLocation release];
}
Everything loaded OK, scrolling and zooming worked. However, when I zoom in mapview (about 5-10 times bigger), and after that, zoom out again to the first size, some annotations locations are changed (See the bottom annotation).
I don't post the function viewForAnnotation here because it's not called, i just zoom in/out and it happends.
http://cC8.upanh.com/27.800.35078007.mF80/1.png http://cC9.upanh.com/27.800.35078008.AWG0/2.png
Oh, you are right. I solved problem after see again viewForAnnotation function. In my old code, when calling [mapView dequeueReusableAnnotationViewWithIdentifier:identifier], i just set identifier to "Image" for all annotations. It makes this function get a random annotation each time. Solved by specifying the true identifier for each annotation. Thanks for your warning!

show map callout through code in iPhone

I have tried several things but am unable to solve it out.
I have 10 custom annotations on the map depending upon the area visible.
Now I have 2 buttons next and previous. Clicking on which the callout of annotation must get displayed.
i.e if i click on next buton then callout of annotation 1 will appear and when i click next again then the callout of first will hide and callout of second will appear.
I have tried out
[self.mapView selectAnnotation:self.nextSelectedAnnotationView.annotation animated:YES]
and
[self.mapView deselectAnnotation:self.selectedAnnotationView.annotation animated:YES];
But the main problem is how to get the annotation here??
I have tried NSArray* selectedAnnotations=self.mapview.annotations to get the annotations array
id annotationView =[selectedAnnotations objectAtIndex:i];
[self.mapView selectAnnotation:annotationView animated:YES];
But no luck :(
Any other way to solve my issue.??
it may help you.
NSArray *selectedAnnotations = mapView.selectedAnnotations;
for(id annotationView in selectedAnnotations) {
[mapView deselectAnnotation:[annotationView annotation] animated:NO];
}

Get info on a mapview selected annotation

I have annotations on a mapview and a callout with a button on each. What I need to do is grab properties from this callout, ie. the title, but logging this line:
NSLog(#"%#", mapView.selectedAnnotations);
returns <AddressAnnotation: 0x1bdc60> which obviously gives me no useful info...
My question is, how can I access the properties of a selected annotation callout?
This is how
for (id annotation in mapView.annotations) {NSLog([annotation title]);}
Here is what I did in the annotationviewClick function:
Hope this helps
-(IBAction) annotationViewClick:(id) sender{
[self.view addSubview:LoadingView];
Annotation *ann = [myMap.selectedAnnotations objectAtIndex:([myMap.selectedAnnotations count]-1)];
NSLog(#"Selected:%#", [ann tag]);
}
mapView.selectedAnnotations returns an array of anotations. You should access its items to get info.