Any demo example Multiple Map Annotation using plist?

后端 未结 2 1845
长发绾君心
长发绾君心 2020-12-22 11:13

i want to use Map annotation i have plist list in which i have data and i want to show data on map using annotation. I have the demo example MapCallsout but i have to read p

相关标签:
2条回答
  • 2020-12-22 11:28

    Here's the sample for retrieving data from plist file, the data will store into NSArray. You can show annotations using this array.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"yourPlistFile.plist"];
    NSLog(fooPath);
    NSArray *contentArray = [NSArray arrayWithContentsOfFile:fooPath];
    NSLog(@"%@",contentArray);
    
    0 讨论(0)
  • 2020-12-22 11:29

    First get All The Locations from Plist and put them into an Array.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"yourPlistFile.plist"];
    NSLog(fooPath);
    NSArray *contentArray = [NSArray arrayWithContentsOfFile:fooPath];
    NSLog(@"%@",contentArray);
    

    Use Below code for showing multiple annotations on your map.

    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10];
            for (id annotation in yourMapView.annotations)
                if (annotation != yourMapView.userLocation)
                    [toRemove addObject:annotation];
            [yourMapView removeAnnotations:toRemove];
    
    
            yourMapView.delegate = self;
    
            [yourMapView setMapType:MKMapTypeStandard];
    
            MKCoordinateRegion region;
    
            MKCoordinateSpan span;
            span.latitudeDelta=0.2;  
            span.longitudeDelta=0.2; 
    
    
            LocationClass *locationData=[[LocationClass alloc]init];
    
    
    
            for (int k=0; k<contentArray.count; k++) {
    
                locationData=[contentArray objectAtIndex:k];
    
                CLLocationCoordinate2D location; 
    
                region.span = span;
                region.center = location;
    
    
                location.latitude =[locationData.latitude doubleValue];
                location.longitude = [locationData.longitude doubleValue];
    
                AnnView *mapPoint = [[AnnView alloc] initWithLocation:location];
    
                mapPoint.title=[NSString stringWithFormat:@"%@ @",locationData.Title];
                mapPoint.subtitle=[NSString stringWithFormat:@"%@ ",locationData.Subtitle];
    
                [yourMapView addAnnotation:mapPoint];
    
                mapPoint = nil;
    
    
                [yourMapView setRegion:region animated:YES];
                [yourMapView regionThatFits:region];
    
    
            }
    
            [self zoomToFitMapAnnotations:yourMapView]; 
    

    Then Write the delegate Method Like Below Code.

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
    
    
        MKAnnotationView * annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annot"];
        if (!annotationView) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annot"] ;
            annotationView.canShowCallout = YES;
        }
        else {
            annotationView.annotation = annotation;
        }
    
            annotationView.image = [UIImage imageNamed:@"pinimage.png"];
    
        return annotationView;
    }
    
    0 讨论(0)
提交回复
热议问题