meghan66 meghan66 - 1 month ago 10
Objective-C Question

How do I create multiple markers for google maps in my iOS app?

I'm unable to find an answer as to how I place several markers on a google map in an iOS app. I know it can be done but I don't know how to do it. This is using some google documentation but my app will display NYC locations.
Here's my very-wrong code:
In GoogleMapsViewController.m I have the following code:

// GoogleMapsViewController.m
// GoogleMaps1
//
// Created by Meghan on 2/1/14.
// Copyright (c) 2014 Meghan. All rights reserved.
//

#import "GoogleMapsViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@interface GoogleMapsViewController ()

@end

@implementation GoogleMapsViewController {
GMSMapView *mapView_;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
{
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;

// Creates a marker in the center of the map (center for Sydney).

NSMutableArray *markersArray = [[NSMutableArray alloc] init];
for(int i=0;i<2;i++) {
id sydney = [markersArray objectAtIndex:0];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.map = mapView_;

id newcastle = [markersArray objectAtIndex:1];
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(-32.92, 151.78);
marker1.title = @"Newcastle";
marker1.snippet = @"Australia";
marker1.map = mapView_;
}
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Answer

I found a way to make it work. I simply added the location in the same format. Here's my code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    {
        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                                longitude:151.20
                                                                     zoom:6];
        mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
        mapView_.myLocationEnabled = YES;
        self.view = mapView_;

        // Creates a marker in the center of the map.
        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
        marker.title = @"Sydney";
        marker.snippet = @"Australia";
        marker.map = mapView_;

        GMSMarker *marker1 = [[GMSMarker alloc] init];
        marker1.position = CLLocationCoordinate2DMake(-32.92, 151.78);
        marker1.title = @"Newcastle";
        marker1.snippet = @"Australia";
        marker1.map = mapView_;
    }
}