加载在线数据非常慢,难以推送

时间:2013-09-24 15:40:59

标签: ios map mkannotation nsjsonserialization

首先,我正在运行一个Map页面,它只显示每个商店的地图上的图钉。我在地图上只运行了一个引脚,在我将超过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;
}

1 个答案:

答案 0 :(得分:1)

简单方法是在后台线程中加载数据,然后在数据完全加载时将其显示在地图上。您可以在任何视图控制器中执行此操作,这意味着您可以在父视图控制器中执行此操作,并在获得响应时在地图视图控制器上更新。 或者在视图上加载方法在后台加载数据并在加载时更新它。这种方法不会保留您的UI。你可以使用像这样的块

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

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