如何在iOS应用中为Google地图创建多个标记?

时间:2014-02-03 16:02:09

标签: ios objective-c google-maps

我无法找到关于如何在iOS应用中的谷歌地图上放置几个标记的答案。我知道可以做到,但我不知道该怎么做。这是使用一些谷歌文档,但我的应用程序将显示纽约的位置。 这是我非常错的代码: 在GoogleMapsViewController.m中,我有以下代码:

//  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

2 个答案:

答案 0 :(得分:1)

我找到了让它发挥作用的方法。我只是以相同的格式添加了位置。这是我的代码:

- (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_;
    }
}

答案 1 :(得分:0)

试试吧

- (void)viewDidLoad {
    [super viewDidLoad];

    Lat=[NSArray arrayWithObjects:@"22.5851",@"22.5830002",@"22.5831",@"22.3932",@"22.3409", nil];
         Long=[NSArray arrayWithObjects:@"88.3468",@"88.33729089999997",@"88.2833",@"87.7427",@"87.3258", nil];
       GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:22.5851
                                                            longitude:88.3468
                                                                 zoom:6];
     GMSMapView *mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView_.myLocationEnabled = YES;
    self.view = mapView_;
      for (int i=0; i<[Lat count]; i++) {



    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake([[Lat objectAtIndex:i]doubleValue], [[Long objectAtIndex:i]doubleValue]);
    marker.title = @"Any Titel";
    marker.snippet = @"India";
    marker.map = mapView_;

}
}