Duplicated MKAnnotations when charging web service in xCode - iphone

I make annotations from a response JSON from a Web service.
I can load the pins in the map, but when I move the position of the center I have to reload a new bunch of annotations and delete the old ones. When I do the method it only charges the pins and if I move the center it does the same and recharge.
I've tried a lot of methods like this one...
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
But it only, does this: I gets to the center of the map and when I move the map it returns to the start and I can't zoom the map cause it reloads and loops.

To remove all annotations, just run this before you add the new ones..
NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:15];
for (id annotation in mapView.annotations){
if (annotation != mapView.userLocation)
[toRemove addObject:annotation];
[mapView removeAnnotations:toRemove];

Related

Remove MKAnnotation from MapView

Ok, I can add pin on my map by LongPress on any place of map. Now I need to delete pins. So I want next: when I click on my pin, appear the name of pin and a little button with cross ((X) like in all apps to close), when user will click this button (X) - pin must be deleted. Can I do this? Or maybe there is another simple way to delete pin for user without go to detailview about this pin?
For remove all annotation use this code.
[yourMapView removeAnnotation:yourMapView.annotations];
For remove one annotation just implement logic For example...
First remove all annotation and also remove your selected pin data from the array and after add this new array and add annotation..
// REMOVING ALL ANNOTATION
for (id <MKAnnotation> myAnnot in [objMapView annotations])
{
if (![myAnnot isKindOfClass:[MKUserLocation class]])
{
[objMapView removeAnnotation:myAnnot];
}
}

How to display and connect multiple locations with a route with annotations in an MKMapView inside an iPhone App?

I need to display a MKMapView with more than 4 locations with different Annotations and a route connecting the locations. I have tried to display the multiple locations inside a MKMapView but i still not able to find out on how to connect the locations with a route. I am also trying to get this checked if i have implemented it in a right way. I have created a "CLLocationCoordinate2D" and then added a lat and long similarly for 4 points. I have created a custom object which implements MKAnnotation and returning a location .
CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(40.7180583 ,-74.007109);
CLLocationCoordinate2D coordinate2 = CLLocationCoordinate2DMake(40.716355 ,-74.006816);
CLLocationCoordinate2D coordinate3 = CLLocationCoordinate2DMake(40.715281 ,-74.005485);
CLLocationCoordinate2D coordinate4 = CLLocationCoordinate2DMake(40.71559 ,-74.003114);
AnnotationPoints *location1 = [[AnnotationPoints alloc] initWithCoordinate:coordinate1];
AnnotationPoints *location2 = [[AnnotationPoints alloc] initWithCoordinate:coordinate2];
AnnotationPoints *location3 = [[AnnotationPoints alloc] initWithCoordinate:coordinate3];
AnnotationPoints *location4 = [[AnnotationPoints alloc] initWithCoordinate:coordinate4];
NSArray *poiArray = [[NSArray alloc] initWithObjects:location1,location2,location3,location4,nil];
[mapView addAnnotations:poiArray];
//Inside the Annotation Class initWithCoordinate Method is implemented this way:-
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
My concern here is i need to create a Annotation Point for every Location. Is there any alternative that i can load all the points at a single place. And another difficulty here is the route connecting all the multiple points. Any help on this? Thanks a lot
The way you are adding the annotations is fine.
Not sure what your concern is and what you mean by "all the points at a single place".
If you want pins/annotations at several places, you have to create a separate annotation object for each place.
Drawing a route connecting those locations requires creating an overlay (not an "annotation").
You want to add an MKPolyline to the map for which you will specify the list of coordinates.
To draw the polyline, you don't need to also add annotations at each coordinate (but you could if you want to).
Creating and adding an MKPolyline and its corresponding MKPolylineView is very similar to MKPolygon and MKPolygonView. See this question for an example:
iPhone MKMapView - MKPolygon Issues

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.

Reload mapView with new pins iphone dev?

I am developing an application in which i need to show multiples events location at same time and i have two types of events(my events and all events). I give opportunity to switch between my events and all events through segment control, First it show all events location and when user switch to my events it'll delete or hide all events location(pins) and show my events location pins on same map view. I've bit confusion how to handle this to reload or refresh map view with new pins?
You may try this...
//Remove or hide all annotations
for (id annotation in mapView.annotations) {
if (annotation != mapView.userLocation) {
[[mapView viewForAnnotation:annotation] setHidden:YES]; // You can remove as well
}
}
/code to remove annotations instead of hiding/
NSMutableArray *listRemoveAnnotations = [[NSMutableArray alloc] init];
for (id annotation in mapView.annotations) {
if (annotation != mapView.userLocation) {
[listRemoveAnnotations addObject:annotation];
}
}
[mapView removeAnnotations:listRemoveAnnotations];
[listRemoveAnnotations release];
When all annotations are dealt with then:
if all annotations are hidden, un-hide the annotations you wish to show.
if all annotations are removed, add annotations you wish to show.

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!