1

I am having problems plotting multiple markers with Google Maps SDK for iOS (ver. 1.5.0). I am new to objective c (using Xcode ver 4.6.3) and the Google Maps SDK so I may be missing something obvious. Also I'm using iOS 6.1 simulator. I'm trying to learn by doing.

I have spent several days searching and have found several threads that have dealt with this topic, but none of the solutions work for me. The problem that I'm having is that my markers are overwriting each other. I created an NSArray, locations, that will have 4 columns and unknown rows. The columns are latitude, longitude, name, address.

for(int i=0;i<[locations count];i++){
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.0823
                                                        longitude:-74.2234
                                                             zoom:7];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.view = mapView_;
    mapView_.myLocationEnabled = YES;
    mapView_.mapType = kGMSTypeHybrid;
    mapView_.settings.myLocationButton = YES;
    mapView_.settings.zoomGestures = YES;
    mapView_.settings.tiltGestures = NO;
    mapView_.settings.rotateGestures = NO;
    NSString *lat = [[locations objectAtIndex:i] objectAtIndex:0];
    NSString *lon = [[locations objectAtIndex:i] objectAtIndex:1];
    double lt=[lat doubleValue];
    double ln=[lon doubleValue];
    NSString *name = [[locations objectAtIndex:i] objectAtIndex:2];
    NSMutableArray *markersArray = [[NSMutableArray alloc] init];
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.appearAnimation=YES;
        marker.position = CLLocationCoordinate2DMake(lt,ln);
        marker.title = name;
        marker.snippet = [[locations objectAtIndex:i] objectAtIndex:3];
        marker.map = mapView_;

       [markersArray addObject:marker];

}
Community
  • 1
  • 1

1 Answers1

5

I see something wrong that's possibly related. You're overwriting markersArray every time you iterate through the locations array in the for loop. Instantiate markersArray outside of the for loop.

Could you try to NSLog the coordinates of each marker you're trying to plot?

If the coordinates are the same, the marker should plot right on top of each other making it appear that markers are being overridden, but they're just on top of each other.

Log the count of the locations and markersArray after you're done to make sure they're equal to each as a quick check.

*Edit: I see your problem. You're overriding your MapView every time you iterate through your for loop.

Try something like this:

// Create a markersArray property
@property (nonatomic, strong) NSMutableArray *markersArray;
// Create a GMSMapView property
@property (nonatomic, strong) GMSMapView *mapView_;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupMapView];
    [self plotMarkers];
}

// Lazy load the getter method
- (NSMutableArray *)markersArray
{
    if (!_markersArray) {
        _markersArray = [NSMutableArray array];
    }
    return _markersArray;
}

- (void)setupMapView
{
    self.mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.view = self.mapView_;
    self.mapView_.myLocationEnabled = YES;
    self.mapView_.mapType = kGMSTypeHybrid;
    self.mapView_.settings.myLocationButton = YES;
    self.mapView_.settings.zoomGestures = YES;
    self.mapView_.settings.tiltGestures = NO;
    self.mapView_.settings.rotateGestures = NO;

    // You also instantiate a GMSCameraPosition class, but you don't add it to your mapview
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.0823
                                                            longitude:-74.2234
                                                                 zoom:7];

}

- (void)plotMarkers
{
    // I don't know how you're creating your locations array, so I'm just pretending
    // an array will be returned from this fake method
    NSArray *locations = [self loadLocations];
    for (int i=0; i<[locations count]; i++){

        NSString *lat = [[locations objectAtIndex:i] objectAtIndex:0];
        NSString *lon = [[locations objectAtIndex:i] objectAtIndex:1];
        double lt=[lat doubleValue];
        double ln=[lon doubleValue];
        NSString *name = [[locations objectAtIndex:i] objectAtIndex:2];

        // Instantiate and set the GMSMarker properties
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.appearAnimation=YES;
        marker.position = CLLocationCoordinate2DMake(lt,ln);
        marker.title = name;
        marker.snippet = [[locations objectAtIndex:i] objectAtIndex:3];
        marker.map = self.mapView_;

        [self.markersArray addObject:marker];

    }
}
Matt Tang
  • 1,297
  • 11
  • 17
  • Thanks for you input. I'm gonna have to take some time to look through your answer. I did get mine working though because, like you mentioned, I was loading my mapView everytime through the loop. – Michael Craven Oct 03 '13 at 19:41