我正在开发一个在地图上查看图钉的应用程序
该应用程序正在使用以下代码从SQLite数据库读取引脚:
- (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];
}
}
}
}由于我有超过10,000品脱,这个应用程序的工作非常慢。
我的问题是:
1-有什么方法可以让这个过程更快吗?就像在处理它们之前只选择半径内最近位置的引脚一样?请提供代码。
2-如果我缩小了,我怎么能掉更多的引脚?
发布于 2011-08-23 22:35:33
首先,从SQL数据库中获取当前需要的值。我不认为你可以在app启动后一次查看所有10000个点。因此可以根据对象的纬度和经度对特定区域进行选择。当用户拖动地图时,使用当前可见的SQL中的图钉更新您的数据模型。
通过addAnnotations:将注释作为批处理添加,这可能会给您增加一些速度。
发布于 2011-08-23 22:24:58
要做的第一件事是分析你的应用程序,找出瓶颈在哪里,结果可能会让你感到沮丧,如果你不这样做,你可能会花费大量时间追求很少或根本没有性能提升。
除此之外,这里有一个很有帮助的页面,提供了一些让SQLite尽可能快地工作的一般建议:
http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html#intro
https://stackoverflow.com/questions/7153137
复制相似问题