M
M
Max Onflic2014-10-14 16:45:53
Objective-C
Max Onflic, 2014-10-14 16:45:53

The problem is creating multiple markers in the map, it only creates the last one. What options?

I am using Google Maps SDK for iOS 1.8.1. Code below. Only the last marker is created. The reason is not clear.

@interface BanksViewController ()

@property (nonatomic, retain) CLLocationManager *locationManager;


@end

@implementation BanksViewController
{
GMSMapView *mapView_;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_icon.png"]
                                                                             style:UIBarButtonItemStyleBordered target:self action:@selector(showMenuViewController)];
    
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"Банкоматы и банки";
    
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    
    
    [self.locationManager startUpdatingLocation];
    
    CLLocation *location = [_locationManager location];
    CLLocationCoordinate2D coordinateCurrent = [location coordinate];
   
    
    
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:coordinateCurrent zoom:14];
    
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    
    
    
    GoogleMapsView.myLocationEnabled = YES;
    GoogleMapsView.settings.myLocationButton = YES;
    GoogleMapsView.camera = camera;
    
    GoogleMapsView.delegate = self;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        ParserDelegate* delegate = [ParserDelegate new];
        
        
        NSString* coordString = [[NSBundle mainBundle] pathForResource:@"coordinates" ofType:@"xml"];
        NSData *xmlData = [NSData dataWithContentsOfFile:coordString];
        
        
    NSXMLParser* parser = [[NSXMLParser alloc] initWithData:xmlData];
    [parser setDelegate:delegate];
    [parser parse];
    
    while ( ! delegate.done )
        sleep(1);
    
    if ( delegate.error == nil ) {
        
        NSArray *itemsAll=delegate.items;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
        for (int i=0; i<itemsAll.count; i++) {
        
           
           //сюда все приходит, проверил, массив из 7 словарей и берем dictionaty по i
            NSDictionary *temp=itemsAll[i];
            
            float latitude=[[temp objectForKey:@"latitude"] floatValue];
            float longitude=[[temp objectForKey:@"longitude"] floatValue];
            
            NSString *city=[temp objectForKey:@"city"];
            NSString *street=[temp objectForKey:@"street"];
            NSString *building=[temp objectForKey:@"building"];
            
            GMSMarker *marker = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake(latitude, longitude)];
            
            marker.title =[temp objectForKey:@"name"];
            marker.snippet = [NSString stringWithFormat:@"%@, %@ %@", city,street,building];
            
            marker.map = GoogleMapsView;
            
          }
            
          GoogleMapsView=mapView_;
            
          });
            
            
        
        
    } else {
        // если была - выводим ошибку
        NSLog(@"Error: %@", delegate.error);
    }
    
    });
    
}



- (UIView*)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
    
    UIViewController *controller=[[UIViewController alloc] initWithNibName:@"CustomViewForATM" bundle:nil];
    CustomViewForATM *customView = (CustomViewForATM*)controller.view;
    [mapView_ addSubview:customView];
    return customView;
}


// метод, определяющий, что гуглокарты должны отображаться внутри вида GoogleMapsView на экране
- (void) setMapView:(GMSMapView *)mapView {
    if (!mapView) {
        mapView = [[GMSMapView alloc] initWithFrame:mapView.bounds];
    }
    GoogleMapsView = mapView;
}


@end

Outcome - the last marker stands, the rest do not. I tried not to allocate memory. Remove initialization from the loop. Nothing helps. Puts one marker, not 7.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
My joy, 2014-10-14
@maxonflic

I don’t rummage in objective c, but I can suggest the following possible solutions:
perhaps all markers are added, just with the same coordinates and superimposed on each other, which makes it seem that he is alone. I made a conclusion based on the fact that all the variables in the cycle start with asterisks, but for some reason the coordinates don’t (again, don’t throw stones - I don’t rummage)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question