标记未显示在谷歌地图上

时间:2015-09-24 05:37:51

标签: ios objective-c google-maps gmsmapview

我在iOS中使用GoogleMap SDK来显示用户的当前位置。谷歌地图显示在我的视图上,但标记不可见。我不知道我在这里做错了什么。

这是我的代码

#import <GoogleMaps/GoogleMaps.h>
@interface firstViewController () {
   // GMSMapView *mapView_;
    IBOutlet GMSMapView *mapView_;
}
@end

@implementation firstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 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 = NO;
    [self.view addSubview: 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_;
 }

2 个答案:

答案 0 :(得分:2)

为什么要使用CGRectZero框设置地图?

mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = NO;
[self.view addSubview: mapView_];

如果将其框架设置为CGRectZero,则地图将不可见。首先,尝试通过给它一个不同的框架来使mapView可见。

答案 1 :(得分:0)

我不知道问题出在哪里,但我认为这种方法对我在我的应用中实现的方式有帮助。

- (void)getCamerasWithNortheast:(CLLocationCoordinate2D)neCoord southwest:(CLLocationCoordinate2D)swCoord fromDutation:(int)fromDuration
{
    NSString* urlString = nil;
    if(fromDuration == 0)
    {
        urlString = [NSString stringWithFormat:@"/api/v1/camera?bounds=%f,%f,%f,%f&limit=100", swCoord.longitude, swCoord.latitude, neCoord.longitude, neCoord.latitude];
    }
    else
    {
        urlString = [NSString stringWithFormat:@"/api/v1/camera?bounds=%f,%f,%f,%f&limit=100&fromDuration=%d", swCoord.longitude, swCoord.latitude, neCoord.longitude, neCoord.latitude,fromDuration];
    }

    _loadingCameras = YES;
    dispatch_async(dispatch_get_main_queue(), ^{
        //[_activitySpinner startAnimating];
    });

    HttpRequest* request = [HttpRequest requestWithRelativePath:urlString];
    [HttpResponse processAsynchronousRequest:request onCompletion:^(HttpResponse* response)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSMutableArray* newCameras = [[NSMutableArray alloc] init];
            newCameras = [response.responseJSON objectForKey:@"cameras"];
            for (NSDictionary* camera in newCameras)
            {
                LiveCameraAnnotation* annotation = [[LiveCameraAnnotation alloc] initWithJSON:camera];
                GMSMarker *marker = [[GMSMarker alloc] init];
                marker.position = annotation.coordinate;
                marker.title = annotation.camera.name;
                 marker.appearAnimation = kGMSMarkerAnimationPop;
                marker.icon = [GMSMarker markerImageWithColor:[UIColor purpleColor]];
                marker.userData = @{@"annotation":annotation};
                marker.map = mapView_;

                // Check if launched with camera
                if (_cameraData && [annotation.camera.uuid isEqualToString:_cameraData.uuid]){
                     [mapView_ setSelectedMarker:marker];
                    _cameraData = nil;
                }
                 marker = nil;
            }
            _loadingCameras = NO;
        });
    }];
}