2013-09-24 4 views
0

먼저 모든지도의지도에 핀 만 표시하는지도 페이지를 실행하고 있습니다. 나는지도 위에 단 하나의 핀만을 달고 있었고, 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; 
} 
+0

문제는 주 스레드에서 수행하는 것입니다. 이러한 히브 작업은 다른 백그라운드 스레드에서 수행해야하며 기본 UI 흐름을 차단하지 않아야합니다. –

+0

'jsonArray'를 통해 두 개의 중첩 된'for i' 루프가있는 것처럼 보입니다. 왜? 'jsonArray'에 25 개의 항목이 있다면, 명백한 이유없이 25 * 25 = 625 반복을 의미합니다. 또한, 왜 당신은 내부 루프 _ 반복마다'CLLocationManager'를 할당하고 시작합니까? 루프 외부에서 위치 관리자 _once_를 할당하고 시작하십시오. _each_ 반복에서'setRegion' 또한 불필요합니다 (루프 외부에서 _once_합니다). – Anna

+0

@AnnaKarenina 루프 외부에서'CLLocationManger'와'setRegion'을 가져 왔습니다. 나는 그것이 훨씬 빠르다고 느낍니다. 'jsonArray' 루프에서 두 개가 아닌 하나의 루프를 사용하라고 말하고 있습니까? 저에게 알려주세요. 고마워 –

답변

1

간단한 방법은 다음 데이터가 완전히로드지도에 표시 될 때 부하 백그라운드 스레드에서 데이터 및입니다. 보기 컨트롤러 중 하나에서이 작업을 수행 할 수 있습니다. 즉, 부모보기 컨트롤러에서 할 수 있고 응답을받은 경우지도보기 컨트롤러에서 업데이트 할 수 있습니다. 또는보기에서로드 메서드가 백그라운드에서 데이터를로드하고로드 할 때 업데이트합니다. 이 접근 방식은 UI를 보유하지 않습니다. 이 같은 블록을 사용할 수 있습니다

dispatch_async(queue, ^{ 
//load data from server 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     //Update map 
    }); 
});