Moving annotations in MKMapView according to server data (latitude and longitude)

In my application, I show the location of another person, getting their location (latitude and longitude) from the server at a certain interval.

After receiving, I need to delete all annotations and discard new annotations based on server data.

But it looks very inefficient, because when we already created the annotation for the same user, I already delete and add the same user annotation.

So, I want to know if we can move MKAnnotation from one coordinate to another? I tried the "setCoordinate" property, but could not implement it.

Also, this is not "touch and drag." Annotations move themselves when the application receives data (latitude and longitude) from the server.

+4
source share
2 answers

Why is this a performance issue? Did you rate him? You simply change the array of objects without even creating new instances.

AnnotationViews on MapView will be reused. There should not be any problems.

0
source

To change the coordinate of an annotation without deleting, re-creating and adding it again, create an annotation class in which the coordinate property will be set. An easy way to do this is to define the coordinate property as follows:

 @property (nonatomic, assign) CLLocationCoordinate2D coordinate; 

Alternatively, instead of defining your own class, you can also use the built-in MKPointAnnotation class, which has a custom coordinate .

First you create and add annotations as usual. For instance:

 MKPointAnnotation *pa = [[MKPointAnnotation alloc] init]; pa.coordinate = someCoordinate; pa.title = @"User1"; [mapView addAnnotation:pa]; [pa release]; 

Later, when you receive an update from the server and change the coordinates for changing "User1", you can either look for this user annotation in the map view annotations array, or store the links to the annotations in some other structure, provides quick access using a key like NSMutableDictionary .

After you find the annotation, you simply set the coordinate annotation property to a new value, and the map view automatically moves the annotation view to a new location.

This example searches for the array annotations :

 for (id<MKAnnotation> ann in mapView.annotations) { if ([ann.title isEqualToString:@"User1"]) { ann.coordinate = theNewCoordinate; break; } } 

If you want to find annotation using another, custom property of your annotation class (for example, some int named userIdNumber):

 for (id<MKAnnotation> ann in mapView.annotations) { if ([ann isKindOfClass:[YourAnnotationClass class]]) { YourAnnotationClass *yacAnn = (YourAnnotationClass *)ann; if (yacAnn.userIdNumber == user1Id) { yacAnn.coordinate = theNewCoordinate; break; } } } 

The above is just an example of how to change the coordinate. If you have many annotations with changing coordinates, I do not recommend looking for them in turn. Instead, you can save the links in the dictionary and quickly browse them using the key value.

+3
source

Source: https://habr.com/ru/post/1399727/


All Articles