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;
}
4

1 回答 1

1

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

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

    dispatch_async(dispatch_get_main_queue(), ^{ 
        //Update map
    });
});
于 2013-09-24T15:52:10.280 回答