i am working on an app that viewing pins on a map
the app is reading the pins from an SQLite data base using this code:
- (void) readDB
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *filename = @"places.db";
NSString *filedocPath = [docDir stringByAppendingPathComponent:filename];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:filedocPath];
if(success == NO)
{
NSString *configPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename];
[fileManager copyItemAtPath:configPathFromApp toPath:filedocPath error:nil];
[fileManager release];
}
FMDatabase* db = [FMDatabase databaseWithPath:filedocPath];
if (![db open]) {
NSLog(@"Could not open db.");
return;
}
// kind of experimentalish.
[db setShouldCacheStatements:YES];
/*read*/
for(int i = 0; i <colNum; i++)
{
[poi_Infos addObject:[[NSMutableArray alloc] init]];
}
NSString *queryString=[NSString stringWithFormat:@"select Category as %@,* from Places",categoryName];
FMResultSet *rs = [db executeQuery:queryString];
while ([rs next])
{
// just print out what we've got in a number of formats.
[ [poi_Infos objectAtIndex:0] addObject:[rs stringForColumn:@"Company Name"]];
[ [poi_Infos objectAtIndex:1] addObject:[NSNumber numberWithDouble:[[rs stringForColumn:@"Latitude"] doubleValue]] ];
[ [poi_Infos objectAtIndex:2] addObject:[NSNumber numberWithDouble:[[rs stringForColumn:@"Longitude"] doubleValue]] ];
}
[rs close];
[db close];
//NSLog(@"%@,%@", [[poi_Infos objectAtIndex:0] objectAtIndex:0],[[poi_Infos objectAtIndex:1] objectAtIndex:0]);
}
- (void) displayLocalPOI
{
CLLocationCoordinate2D CurrentLocation;
CurrentLocation=map.userLocation.coordinate;
//set the radius in km so that you will get the nearest location in that radius
double radius=localSearchRadius;
int num = [[poi_Infos objectAtIndex:0] count];
for(int i = 0; i < num; i++)
{
double y = [[[poi_Infos objectAtIndex:1] objectAtIndex:i] doubleValue];
double x = [[[poi_Infos objectAtIndex:2] objectAtIndex:i] doubleValue];
if(y>=CurrentLocation.latitude-(radius/111)&&y<=CurrentLocation.latitude+(radius/111))
{
if(x>=CurrentLocation.longitude-(radius/111)&&x<=CurrentLocation.longitude+(radius/111))
{
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(
(CLLocationDegrees)y,
(CLLocationDegrees)x);
NSString *title = [[poi_Infos objectAtIndex:1] objectAtIndex:i];
UserAnnotation *userAnnotation = [[UserAnnotation alloc] initWithCoordinate:coord];
userAnnotation.title = title;
[self.mapAnnotations addObject:userAnnotation];
[userAnnotation release];
}
}
}
}
as i have more than 10,000 pints, the app work very slowly.
My question is:
1- is there any way to make the process faster? like select only pins in the nearest location in a radius before handling them? please provide a code.
2- how can i drop more pins if i zoom out?
First of all, get from SQL database values what you need at current moment. I don’t think what you can view all 10000 points at once after app started. So make selection for specific region by objects latitude and longitude. And when user drag map, update your data model with pins from SQL which visible at current moment.
Add annotation as batch through
addAnnotations:this may add some speed to you.