I have this situation where my app crashes when I zoom out the map.
The problem arises because of the large number of annotations that I'm adding. Please have a look at my code below :
- (void) plotUsersInMap
{
for (id<MKAnnotation> annotation in self.mapView.annotations) {
[self.mapView removeAnnotation:annotation];
}
NSUInteger count = //get total count
NSLog(#"count * %d", count);
for (int i = 0; i < count; i++)
{
NSNumber *latitude = //get latitude from json
NSNumber *longitude = //get longitude from json
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
#autoreleasepool {
MyLocation *annotation = [[MyLocation alloc] initWithName:#"test" coordinate:coordinate QuestionId:nil];
//annotations are added
[self.mapView addAnnotation:annotation];
}
}
}
Here I'm trying to add more than 400 pins which I think is the cause of crash [probably a memory leak!]. I would like to know if there is any way to add the pins one by one as I zoom out?
Map in initial stage, without any problem :
And when I zoom out :
Try clustering. Basically you group together annotations.
The code repo from the article I linked to: https://github.com/applidium/ADClusterMapView
Related
There are thousands of data that I want to add on google map using clustering.
Problem
It is possible that multiple records may have same latitude and longitude so I want to add those marker in such a way that it doesn't overlap one another. They can be close to each other but not overlapped.
So I have went through the google place api documentation and even googled or the various articles but I didn't found a solution that solves this issue.
Setting pin on map:-
- (void)setPinsToMap {
[self.mapGoogle clear];
for (int i=0; i<arrUsers.count; i++) {
ClassUser *cls =[arrUsers objectAtIndex:i];
double lat;
double long1;
//Here, what to do with the location coordinates?
lat = [cls.strLat doubleValue];
long1 = [cls.strLong doubleValue];
NSLog(#"\n------------------\nname = %# lat = %#, lng = %#\n---------------\n", cls.strUserName, cls.strLat, cls.strLong);
NSString *name = [NSString stringWithFormat:#"%d", i];
id<GMUClusterItem> item = [[POIItem1 alloc] initWithPosition:CLLocationCoordinate2DMake(lat, long1) name:name];
[_clusterManager addItem:item];
[_clusterManager cluster];
}
}
Could any one suggest a way to get this fixed. Any help will be welcomed.
In my application, I was published recorded video from particular location. I can get the list of published location details from another service response. so Initially I got that published location details and displayed it on MapView. In MapView the pins are displayed with Cluster effect, so I used kingpin.
Here below I have loaded the Annotations on Map.
- (void)loadLocations:(NSArray *)arrayValues
{
_annotationArray = arrayValues;
[self.clusteringController setAnnotations:[self reSetannotations]];
[self.mapView setZoomEnabled:YES];
[self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];
KPGridClusteringAlgorithm *algorithm = [KPGridClusteringAlgorithm new];
algorithm.annotationSize = CGSizeMake(25, 50);
algorithm.clusteringStrategy = KPGridClusteringAlgorithmStrategyTwoPhase;
self.clusteringController = [[KPClusteringController alloc] initWithMapView:self.mapView
clusteringAlgorithm:algorithm];
self.clusteringController.delegate = self;
self.clusteringController.animationOptions = UIViewAnimationOptionCurveEaseOut;
[self.clusteringController setAnnotations:[self annotations]];
NSString * lastobjlat;
double miles;
CLLocation * location = [COMMON currentLocation];
lastobjlat = [NSString stringWithFormat:#"%f",location.coordinate.latitude];
miles = 1.;
double scalingFactor = ABS( (cos(2 * M_PI * [lastobjlat floatValue] / 360.0) ));
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor * 69.0);
MKCoordinateRegion region;
region.span = span;
region.center = location.coordinate;
[self.mapView setRegion:region animated:YES];
self.mapView.showsUserLocation = YES;
//Call in the below selectAnnotationAction method when I came to this, after I published new one on existed or new locations
if (COMMON.isRecentPublication == YES) {
COMMON.isRecentPublication = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self selectAnnotationAction];
});
}
}
//To reset the Annotations
- (NSArray *)reSetannotations
{
NSMutableArray *annotations = [NSMutableArray array];
return annotations;
}
//Here I managed location details on my custom marker class MapAnnotation.
- (NSArray *)annotations {
CLLocationCoordinate2D locationPort;
NSMutableArray *annotations = [NSMutableArray array];
NSString *latitude, *longitude;
if ([_annotationArray count] > 0) {
for (int i = 0; i <[_annotationArray count]; i++){
latitude = [NSString stringWithFormat:#"%#",[[_annotationArray objectAtIndex:i] valueForKey:#"latitude"]];
latitude = [latitude stringByReplacingOccurrencesOfString:#"\n" withString:#""];
latitude = [latitude stringByReplacingOccurrencesOfString:#"\t" withString:#""];
longitude = [NSString stringWithFormat:#"%#",[[_annotationArray objectAtIndex:i] valueForKey:#"longitude"]];
longitude = [longitude stringByReplacingOccurrencesOfString:#"\n" withString:#""];
longitude = [longitude stringByReplacingOccurrencesOfString:#"\t" withString:#""];
latitude = [NSString replaceEmptyStringInsteadOfNull:latitude];
longitude = [NSString replaceEmptyStringInsteadOfNull:longitude];
int publicationRatio = [[[_annotationArray objectAtIndex:i] valueForKey:#"publicationRatio"] intValue];
int publicationCount = [[[_annotationArray objectAtIndex:i] valueForKey:#"publicationsCount"] intValue];
int teazLocationId = [[[_annotationArray objectAtIndex:i] valueForKey:#"objectId"] intValue];
BOOL isUpgrade = [[[_annotationArray objectAtIndex:i] valueForKey:#"isUpgraded"] boolValue];
locationPort = CLLocationCoordinate2DMake([latitude doubleValue] ,
[longitude doubleValue]);
//TODO : This is my custom annotation method
MapAnnotation *a1 = [[MapAnnotation alloc] initWithCoordinate:locationPort
tag:i
publicationRatio:publicationRatio
publicationCount:publicationCount
teazLocationId:teazLocationId isUpgraded:isUpgrade];
a1.itemindex = i + 1;
a1.publicationRatio = publicationRatio;
a1.publicationCount = publicationCount;
a1.teazLocationId = teazLocationId;
a1.isUpgraded = isUpgrade;
a1.coordinate = CLLocationCoordinate2DMake([latitude doubleValue] ,
[longitude doubleValue] );
[annotations addObject:a1];
if (COMMON.isRecentPublication == YES) {
if ([COMMON.recentPublicationLocationID isEqual: #(publishedLocationId)]) {
_recentAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:a1 reuseIdentifier:#"cluster"];
}
}
}
}
return annotations;
}
It's not a problem to load Annotations with clustering effect on initial time. But when I published some one from same or different(new also) locations, I need to redirect Map screen(publishing section is other screen) and I need to display that publication detail with Active pin image.
Following steps I made to show the published location detail on Map
I created recentPublicationLocationID as a global variable and store the recentPublishedLocationID from response after published service.
Then this return type method - (NSArray *)annotations(after redirect to mapView I got the location details from another webservice after that it will called),
I have compared with recentPublishedId If existed or not. Then If existed, I have assigned my custom annotation (contains the recentPublished location details) to global MKPinAnnotationView instance - _recentAnnotationView
Then I directly called the didSelectPinAnnotation delegate method from this method - (void)loadLocations:(NSArray *)arrayValues like below,
//Pass recent published location details
if (COMMON.isRecentPublication == YES)
{
COMMON.isRecentPublication = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self selectAnnotationAction];
});
}
//selectAnnotationAction
- (void)selectAnnotationAction {
COMMON.isRecentPublication = NO;
COMMON.recentPublicationLocationID = nil;
[self mapView:self.mapView didSelectAnnotationView:_recentAnnotationView];
}
If I directly passed recentPublishedLocation details to didSelectAnnotationView delegate, I can only show the In Active pin instead of Active pin.
Then I debug with breakpoint why I can see the In active pin only ?
Because In this situation the didselect delegate was called and I can see the Active pin image. But it's only within sec.
Because viewForAnnotation delegate was called quickly for other pin annotations so the selected one goes to unselected state
This is the real problem. How can I overcome this work with clusters ?
Because when I displayed that published location detail correctly on map even it should be work with clustering effect. Yes I will zoom back to see the pins with cluster effect .
Finally I got the solution and achieved my requirement using ClusterKit library instead of Kingpin.
This tool really helpful me against possible to achieve append annotations and customize every thing adopt with my requirements.
So it's more helpful to me. Of course all of you.
And this tool supports Apple & Goole maps with Objective c as well as Swift.
I hope this one more helpful to other developers also.
Thanks :)
In my app, I download a set of point from a web service using json.
Actualizing the app to iOS7, I am experimenting that problem: the locations are downloaded but pins on the map are not painted until user touchs and "moves" the map. Then they appear and all work as in iOS6.
How can I correct that behavior?
EDIT:
AddAnnotation is called at the end of a method that receive data, parse the json and pass them to mylocaction object:
- (void)plotBarPosition:(NSString *)data_string {
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:#"features"];
for (int i = 0; i < [json count]; i++){
/*
PARSING EACH POINT
*/
MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId];
[_mapView addAnnotation:location];
}
}
I tryed also:
[_mapView performSelectorOnMainThread: #selector(addAnnotations:) withObject: location waitUntilDone: NO];
but in this case, annotations do not appear at all.
As this answer and the comment by #RAJA suggest, call addAnnotation: or addAnnotations: on the main thread. You could do this using GCD or performSelectorOnMainThread.
The option with the least changes to your existing code is to call addAnnotation: (singular):
- (void)plotBarPosition:(NSString *)data_string {
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:#"features"];
for (int i = 0; i < [json count]; i++){
/*
PARSING EACH POINT
*/
MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId];
//[_mapView addAnnotation:location];
[_mapView performSelectorOnMainThread:#selector(addAnnotation:)
withObject:location
waitUntilDone:YES];
}
}
Alternatively, to use addAnnotations: (plural), you first add your annotations to a local array and give them to the map view all together in a single call. Changes below are marked with >>>:
- (void)plotBarPosition:(NSString *)data_string {
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:#"features"];
//>>> initialize array to hold annotations...
NSMutableArray *annotationsToAdd = [NSMutableArray array];
for (int i = 0; i < [json count]; i++){
/*
PARSING EACH POINT
*/
MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId];
//>>> comment out direct addAnnotation...
//[_mapView addAnnotation:location];
//>>> add annotation to local array...
[annotationsToAdd addObject:location];
}
//>>> call addAnnotations: (plural) on main thread...
[_mapView performSelectorOnMainThread:#selector(addAnnotations:)
withObject:annotationsToAdd
waitUntilDone:YES];
}
There is a protocol MKAnnotation - you have to determine you own class for annotation with realizing of this protocol:
#interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
Then you have to init MyAnnotation and add it on map:
MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
[myMap addAnnotation:annotation];
Also try to call
[mapView setNeedsDisplay];
or
[mapView setNeedsLayout];
after adding annotations
I have a database that contains 16000 annotations. When the user is zooming out, there are too many points loaded and points overlap. I found a filter with code but i want to load some point only from SQL, not to load all and filter it after. I don't know if there is a stored procedure in iOS or other solution.
This is my code
iphoneScaleFactorLatitude = 3.3;
float iphoneScaleFactorLongitude = 6.38;
float latDelta = self.mapView.region.span.latitudeDelta/iphoneScaleFactorLatitude;
float longDelta = self.mapView.region.span.longitudeDelta/iphoneScaleFactorLongitude;
NSMutableArray *shopsToShow = [[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<[placesToFilter count]; i++) {
MKPointAnnotation *checkingLocation = [placesToFilter objectAtIndex:i];
CLLocationDegrees latitude = [checkingLocation coordinate].latitude;
CLLocationDegrees longitude = [checkingLocation coordinate].longitude;
bool found=FALSE;
for (MKPointAnnotation *tempPlacemark in shopsToShow) {
if(fabs([tempPlacemark coordinate].latitude-latitude) < latDelta &&
fabs([tempPlacemark coordinate].longitude-longitude) <longDelta ){
[self.mapView removeAnnotation:checkingLocation];
found = TRUE;
break;
}
}
if (!found){
[shopsToShow addObject:checkingLocation];
[self.mapView addAnnotation:checkingLocation];
}
}
But they load too many point and my problem is that I don't want to load all the points because each chagement I have to use the algorithm to the sorting.
For some reason, when I use [mapView addOverlay:], nothing happens.
Code:
NSData* dataLat = [[NSUserDefaults standardUserDefaults] objectForKey:#"latKey"];
NSArray* overlayLat = [NSKeyedUnarchiver unarchiveObjectWithData:dataLat];
double lats[overlayLat.count];
NSData* dataLong = [[NSUserDefaults standardUserDefaults] objectForKey:#"longKey"];
NSArray* overlayLong = [NSKeyedUnarchiver unarchiveObjectWithData:dataLong];
double longs[overlayLong.count];
for(int iii = 0; iii < overlayLat.count; iii++)
{
NSNumber* a = (NSNumber*)[overlayLat objectAtIndex:iii];
lats[iii] = [a doubleValue];
}
for(int iii = 0; iii < overlayLong.count; iii++)
{
NSNumber* a = (NSNumber*)[overlayLong objectAtIndex:iii];
longs[iii] = [a doubleValue];
}
int size = (sizeof(lats) / sizeof(lats[0]));
NSLog(#"%d", size);
MKMapPoint points[size];
for(int iii = 0; iii < overlayLong.count; iii++)
{
MKMapPoint point = MKMapPointMake(lats[iii], longs[iii]);
if(lats[iii] != 0)
points[iii] = point;
}
for(int iii = 0; iii < 15; iii++)
{
NSLog(#"Lat (x):%f", points[iii].x);
NSLog(#"Long (y):%f", points[iii].y);
}
MKPolyline* line = [MKPolyline polylineWithPoints:points count:size];
[mapView addOverlay:line];
NSLog(#"finished");
The vast majority of this code just accesses the data and turns it into usable coordinates. My question is (because I know that the coordinates are valid because of the NSLogs), why doesn't anything get added to the mapView when addOverlay: is called? I can post more code if need be. Thanks!
The polylineWithPoints:count: method takes a C array of MKMapPoint structs.
An MKMapPoint is not the same as a CLLocationCoordinate2D (latitude, longitude). You are creating MKMapPoint structs using MKMapPointMake but giving it latitude and longitude values. Such an MKMapPoint is not at the expected location.
Either use MKMapPointForCoordinate to create an MKMapPoint from lat/long coordinates or, probably easier, create a C array of CLLocationCoordinate2D structs and call polylineWithCoordinates:count: instead.
Could it be your 'size' is wrong? Your points array will also have missing items anywhere the source data had 0. I'd go with an NSMutableArray and add items to that if they are deemed valid, then count that at the end and pass it to polylineWithPoints.