如何使用webservice在iOS中显示多个地图注释?

时间:2013-12-16 09:54:11

标签: ios iphone cllocationmanager mkannotation

我正在使用webservice。我收到了XML响应。

我完全读取了我的数据。我想在地图视图上显示该数据,但我无法显示。 viewForAnnotation只调用一次,但必须调用11次。我不知道为什么会这样。

请检查我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 65, 320, 508)];
    mapView.backgroundColor = [UIColor redColor];
    [self.mapView setDelegate:self];
    [self.view addSubview:mapView];
    [self.mapView setShowsUserLocation:YES];

    locationManager = [[CLLocationManager alloc] init];
    [self sendRequestToServer];
}


- (void) startXmlParsing
{

    ///////////////// Start XML PArsing From Here  ////////
    NSXMLDocument* xmlDoc = [[NSXMLDocument alloc] initWithData:_serverData options:0 error:nil]; 
    NSXMLElement* rootElement = [xmlDoc rootElement];   

    NSArray* spotList = [rootElement elementsForName:@"spot"];
    NSLog(@"driversList %lu ", (unsigned long)[spotList count]);   

    int k = [spotList count];
    NSLog(@"k is %d ", k);    // K is 11

    int i = 0;


    // NSXMLElement* firstElement;              
    for (firstElement in spotList) {      objectAtIndex

        i++;

        NSLog(@"i is: %d",i);  // For Checking once again is it call 11 time or not

        str_id            = [firstElement stringValueForNode:@"ID"];
        str_username      = [firstElement stringValueForNode:@"username"];
        str_streetnumber  = [firstElement stringValueForNode:@"streetnumber"];
        str_streetname    = [firstElement stringValueForNode:@"streetname"];
        str_suburb        = [firstElement stringValueForNode:@"suburb"];
        str_postalcode    = [firstElement stringValueForNode:@"postalcode"];
        str_state         = [firstElement stringValueForNode:@"state"];
        str_country       = [firstElement stringValueForNode:@"country"];
        str_lat           = [firstElement stringValueForNode:@"lat"];
        str_lng           = [firstElement stringValueForNode:@"lng"];

        [self click_method];  // This method is calling only once "viewForAnnotation" This  is the problem and I am not able to show my annotations on map View only one annotation is calling on map view. I am calling this methed here for the purpose of showing all the data I collected from webservice to show on the map all at once & together.

    }

}

-(void)click_method{

      // [mapView removeAnnotations:mapView.annotations];

      locationManager.delegate = self;
      locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      [locationManager startUpdatingLocation];
  }


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation

{


     // In This I dont want geo coding I want to give direct lattitute and longitude with  title as I am getting lattitude and longitude from web service now, how to give lat & long directly on Mkmapannotation?


    NSString *str_Address2 = [NSString  stringWithFormat:@"%@,%@,%@,%@,%@,%@",str_streetnumber,str_streetname,str_state,str_country, str_suburb,str_postalcode];
    NSLog(@"Address is: %@",str_Address2);

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    [geocoder geocodeAddressString:str_Address2 completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error) {
        NSLog(@"Fehlar %@", error.localizedDescription);
        return ;
    }

    for (id object in placemarks) {


        CLPlacemark *placemark = object;

        ////////////////////// Title ///////////////////////

        MapAnnotation *mapannotaion = [[MapAnnotation alloc] initWithTitle:[NSString  stringWithFormat:@"%@,%@,%@,%@,%@,%@",str_streetnumber,str_streetname,str_state,str_country, str_suburb,str_postalcode] subtitle:placemark.subLocality  coordinate:placemark.location.coordinate];

        NSLog(@"Postal Code is: %@",placemark.postalCode);
        NSLog(@"Locality is: %@",placemark.locality);
        [self.mapView addAnnotation:mapannotaion];

        MKCoordinateRegion region;
        region.center = mapannotaion.coordinate;

        MKCoordinateSpan span;
        span.latitudeDelta  = 0.01;
        span.longitudeDelta = 0.01;
        region.span = span;

        [self.mapView setRegion:region animated:YES];

    }

}];

// Stop Location Manager
[locationManager stopUpdatingLocation];
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView2 viewForAnnotation:(id  <MKAnnotation>) annotation
{


MKAnnotationView *a = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

if ( a == nil )
    a = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier: @"currentloc" ];


NSLog(@"%f",a.annotation.coordinate.latitude);
NSLog(@"%f",a.annotation.coordinate.longitude);

 CLLocation* currentLocationMap = [[CLLocation alloc] initWithLatitude:a.annotation.coordinate.latitude longitude:a.annotation.coordinate.longitude];
// [self coordinatesToDMS:currentLocationMap];


MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:a reuseIdentifier:@"currentloc"];
if(a.annotation.coordinate.longitude == mapView.userLocation.coordinate.longitude ||  a.annotation.coordinate.latitude == mapView2.userLocation.coordinate.latitude  )
{
    if ([annotation isKindOfClass:MKUserLocation.class])
    {

        return nil;
    }
        }
else
{

}

annView.animatesDrop = YES;
annView.draggable = YES;

annView.canShowCallout = YES;

return annView;

}

2 个答案:

答案 0 :(得分:0)

来自Apple Docs:viewForAnnotation是委托方法,为每个注释对象调用。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation

    Returns the view associated with the specified annotation object.

从您的代码中,您似乎只在didUpdateToLocation方法中制作了一个注释对象:[self.mapView addAnnotation:mapannotaion];

因此iOS系统只提供一个注释视图。

答案 1 :(得分:0)

- (void)centerMap {     MKCoordinateRegion region;

CLLocationDegrees maxLat = -90;

CLLocationDegrees maxLon = -180;

CLLocationDegrees minLat = 120;

CLLocationDegrees minLon = 150;

NSMutableArray *temp=[NSArray arrayWithArray:reports];

for (int i=0; i<[temp count];i++) {
    BasicMapAnnotation*customAnnotation=[[BasicMapAnnotation alloc]init];
    customAnnotation.latitude = [[[temp objectAtIndex:i] valueForKey:@"PostLattitude"]floatValue];
    customAnnotation.longitude =[[[temp objectAtIndex:i] valueForKey:@"PostLongitude"]floatValue];


    UIImage*pinImage=[[UIImage alloc]init];
    NSString *imageUrl=[[temp objectAtIndex:i] valueForKey:@"ProfileImage"];

    if ([imageUrl isEqualToString:@"No Image"]) {
        pinImage=[UIImage imageNamed:@"arrow-icon-1.png"];
    }
    else
    {
        //   pinImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[temp objectAtIndex:i] valueForKey:@"ProfileImage"]]]];
        pinImage=[UIImage imageNamed:@"arrow-icon-1.png"];

    }

    [listOfImagesArray addObject:pinImage];

    CLLocation* currentLocation = (CLLocation*)customAnnotation ;
    if(currentLocation.coordinate.latitude > maxLat)
        maxLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.latitude < minLat)
        minLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.longitude > maxLon)
        maxLon = currentLocation.coordinate.longitude;
    if(currentLocation.coordinate.longitude < minLon)
        minLon = currentLocation.coordinate.longitude;

    region.center.latitude     = (maxLat + minLat) / 2;
    region.center.longitude    = (maxLon + minLon) / 2;
    region.span.latitudeDelta  =  maxLat - minLat;
    region.span.longitudeDelta = maxLon - minLon;
}

[myMapview setRegion:region animated:YES];

}