【发布时间】:2013-09-29 22:28:11
【问题描述】:
首先,我正在运行一个地图页面,该页面仅在每个商店的地图上显示图钉。我在地图上只运行了一个大头针,当我放置超过 25 个大头针后它很快就会推到地图页面上。它现在在该过程中正在做什么,应用程序只需加载引脚位置的所有数据(如我在目标输出中看到的那样),然后推送到下一个屏幕。那么请问我的问题在哪里?
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"";
self.navigationItem.title = @"Mek";
response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/mapLocation.php"]];
if(response!=nil) {
NSError *parseError = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
jsonArray1 = [[NSMutableArray alloc] init];
jsonArray2 = [[NSMutableArray alloc] init];
jsonArray3 = [[NSMutableArray alloc] init];
for(int i=0;i<[jsonArray count];i++)
{
name = [[jsonArray objectAtIndex:i] objectForKey:@"name"];
longitude = [[jsonArray objectAtIndex:i] objectForKey:@"longitude"];
latitude = [[jsonArray objectAtIndex:i] objectForKey:@"latitude"];
[jsonArray1 addObject:name];
[jsonArray2 addObject:longitude];
[jsonArray3 addObject:latitude];
self.locationMap.delegate = self; //set delegate before adding annotations
CLLocationCoordinate2D annotationCoord;
for (int i=0; i < [jsonArray count]; i++)
{
NSDictionary *annotationDictionary = [jsonArray objectAtIndex:i];
name = [annotationDictionary objectForKey:@"name"];
annotationCoord.latitude
= [[annotationDictionary objectForKey:@"longitude"] doubleValue];
annotationCoord.longitude
= [[annotationDictionary objectForKey:@"latitude"] doubleValue];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = name;
annotationPoint.subtitle = [NSString stringWithFormat:@"%f %f", annotationPoint.coordinate.latitude, annotationPoint.coordinate.longitude];
[self.locationMap addAnnotation:annotationPoint];
//------------------------//
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(annotationPoint.coordinate, 50000, 50000);
[self.locationMap setRegion:[self.locationMap regionThatFits:region] animated:YES];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
lat = locationManager.location.coordinate.latitude;
lon = locationManager.location.coordinate.longitude;
}
}
}
else {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"You are not connected to internet" message:@"Please check the internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != locationMap.userLocation)
{
static NSString *defaultPinID = @"myPin";
pinView = (MKAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.image = [UIImage imageNamed:@"pinpinpin.png"];
pinView.canShowCallout = YES;
pinView.enabled = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = infoButton;
}
return pinView;
}
【问题讨论】:
-
问题是你在主线程上做这件事——这样的提升操作应该在另一个后台线程上完成——不阻塞主 UI 流。
-
您似乎有两个嵌套的
for i循环通过jsonArray。为什么?如果jsonArray中有 25 个条目,则意味着 25 * 25 = 625 次迭代,没有明显的原因。另外,为什么要在内部循环的每次迭代中分配和启动CLLocationManager?在循环外分配并启动位置管理器一次。 每个迭代中的setRegion也是不必要的(在循环之外执行一次)。 -
@AnnaKarenina 我已将
CLLocationManger和setRegion置于循环之外。我现在感觉速度快了很多。你是在告诉我在jsonArray循环中使用一个循环而不是两个循环?请告诉我。谢谢 -
我不明白您为什么要循环遍历该数组 N*N 次。看起来你只需要一个循环。是的,这就是我要说的。
-
@AnnaKarenina 有人告诉我这样做,我不记得为什么我一直这样。我已经修复它并且它正在工作谢谢!
标签: ios map mkannotation nsjsonserialization