MkMapKit中显示多个注释视图中的问题

时间:2012-01-19 07:04:51

标签: iphone annotations mapkit

我需要在MKMapKit中显示多个annonation视图..我使用此代码..

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    DisplayMap *ann = [[DisplayMap alloc] init];
    //NSLog(@"xapp.arrplacename:%d",[xapp.arrplacename count]);
    for (i=0;i<=[arrplacename count]-1;i++)
    {

        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
        //arrplacename contains the address of the locations
        NSString *straddress=[arrplacename objectAtIndex:i];
        NSString *strregion=@"";
        NSString *strs=[straddress stringByAppendingString: strregion];
        //  lbladdress.text=strs;
        //  NSString add=strs;
        // here you define the url setting your address
        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",[strs stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

        NSLog(@"US: %@",urlString);

        // then you get the response string here
        NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
        NSLog(@"loaction strings%@",locationString);
        // now you can create an array splitting the response values using the ","
        //[listItems addObject:locationString];
        listItems = [locationString componentsSeparatedByString:@","];

        NSLog(@"A: %@",[xapp.listItems objectAtIndex:2]);

        // define latitude and longitude variables
        double latitude  = 0.0;
        double longitude = 0.0;

        NSString *strlatitude;
        NSString *strlongitude;

        strlatitude=[xapp.listItems objectAtIndex:2];
        strlongitude=[xapp.listItems objectAtIndex:3];

        float stringFloat = [strlatitude floatValue]; 
        float string1Float = [strlongitude floatValue]; 

        // if your array has inside the sctructure you aspect (4 values as described in my previous post) and the first value is like "200" (OK result)
        if([xapp.listItems count] >= 4 && [[xapp.listItems objectAtIndex:0] isEqualToString:@"200"]) 
        {
            // not sure if you need the accurancy parameter
            //accurancy = [[listItems objectAtIndex:1] intValue];

            // here you can get your values

            // latitude = [[listItems objectAtIndex:2] doubleValue];
            //longitude = [[listItems objectAtIndex:3] doubleValue];

            latitude = stringFloat;
            longitude = string1Float;

        }
        else {
            NSLog(@"Error on map");
        }
        region.center.latitude = latitude;
        region.center.longitude = longitude;
        NSLog(@"latitude:%g",region.center.latitude);
        //  region.center.latitude = 42.7157850;
        // region.center.longitude = 27.56470180;

        region.span.longitudeDelta = 0.01f;
        region.span.latitudeDelta = 0.01f;
        [mapView setRegion:region animated:YES];
        [mapView setDelegate:self];
        ann.title = [xapp.arrplacename objectAtIndex:i];
        ann.subtitle = [xapp.arrplacename objectAtIndex:i];
        ann.coordinate = region.center;
        [mapView addAnnotation:ann];

    }

}

我在这段代码中确实遇到了一些问题....所有的coordnates都没有添加到listitems数组中..我只能看到在atlast中添加的对象......

这是我添加的代表,用于在我的地图中显示年代..

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address{

    NSLog(@"ssssss");

    double latitude = 0, longitude = 0;
    NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\":" intoString:nil] && [scanner scanString:@"\"lat\":" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\":" intoString:nil] && [scanner scanString:@"\"lng\":" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;

    return center;
}


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

   MKPinAnnotationView *pinView = nil;
    NSLog(@"pinnview before release %d",[pinView retainCount]);

    if (pinView !=nil) {
        pinView =nil;
        [pinView release];
    }
    NSLog(@"pinnview after release %d",[pinView retainCount]);

    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if(annotation != mapView.userLocation)
    {

        static NSString *defaultPinID = @"your-pin";

        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

        if ( counting < [mapView.annotations count])
        {
            counting++;

            pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];


           /* for(DisplayMap* a in mapView.annotations)
            {
                if (annotation == a){
                    pinView.image =
                    [UIImage imageWithContentsOfFile:
                     [[NSBundle mainBundle] pathForResource:a.image ofType:nil]];   
                }
            }*/
            pinView.centerOffset= CGPointMake(0,-10);
            pinView.canShowCallout = YES;


        }

    }

   // return pinView;


}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    NSLog(@"enter");
    [mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];
}

虽然我在 for 循环中使用[mapView addAnnotation:ann];,但代表只被调用一次...而且它只显示了针对数组中添加的最后一项tatz的引脚...我可以克服这个????

1 个答案:

答案 0 :(得分:0)

你在for循环开始之前只分配和初始化'ann'一次,并且你正在改变它并一次又一次地将它添加到Map中。这就是为什么你只得到一个注释。

将此行移入for循环并尝试,

DisplayMap *ann = [[DisplayMap alloc] init];