【问题标题】:Issue in Displaying Multiple annonations view in MkMapKit在 MkMapKit 中显示多个注释视图的问题
【发布时间】:2012-01-19 07:04:51
【问题描述】:

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

- (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];

    }

}

我在这段代码中确实有一些问题......所有坐标都没有添加到 listitems 数组中......我只能看到最后添加的对象......

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

- (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 的 pin...我怎么能克服这个????

【问题讨论】:

  • NSString *straddress=[arrplacename objectAtIndex:i];这段代码被替换为
  • hello icoder 看到这段代码 ann.title = [xapp.arrplacename objectAtIndex:i]; ann.subtitle = [xapp.arrplacename objectAtIndex:i];并使用这一行 DisplayMap *ann = [[DisplayMap alloc] init];在循环内\

标签: iphone annotations mapkit


【解决方案1】:

在 for 循环开始之前,您只分配和初始化“ann”一次,并且您正在更改相同的内容并将其一次又一次地添加到 Map 中。这就是为什么你只得到一个注释。

将此行移到 for 循环中并尝试,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多