GMSMapView GroundOverlay Cover Entire MapView Bounds

1k views Asked by At

I have setup a UIView to display a specific region of a Map using Google Maps. I now want to add an image overlay on top of this selected region but I dont know how to calculate the correct coordinates for this. I have set the map with a center coordinate, but the Overlay needs NorthWest and South East coordinates. Can someone help please? I am trying to put an image of a race track over some roads.

Below is my code so far:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:14.5809268
                                                       longitude:120.975319
                                                            zoom:16.5
                                                         bearing:90
                                                    viewingAngle:0];

    // Indicating the map frame bounds
    mapView_ = [GMSMapView mapWithFrame:self.mapViewOnScreen.bounds camera: camera];
    mapView_.myLocationEnabled = YES;

    // Add as subview the mapview
    [self.mapViewOnScreen addSubview: mapView_];


    //this is where I need to figure out the coordinates but get stuck...

    CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(40.712216,-74.22655);
    CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(40.773941,-74.12544);
    GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest
                                                                          coordinate:northEast];


    //Add track image over the road map to show a race track - roads need to match up

    UIImage *icon = [UIImage imageNamed:@"Track.jpg"];
    GMSGroundOverlay *overlay =
    [GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:icon];
    overlay.bearing = 0;
    overlay.map = mapView_;

    // Creates a marker in the center of the map.
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(14.5809268, 120.975319);
    marker.title = @"Race Day";
    marker.snippet = @"Manila";
    marker.map = mapView_;

}
1

There are 1 answers

0
MaxK On

I haven't tried this, but after doing some research on the API, I think you can achieve what you want by doing the following:

GMSProjection *projection = mapView_.projection;
GMSVisibleRegion visibleRegion = [projection visibleRegion];
GMSCoordinateBounds *coordinateBounds = [[GMSCoordinateBounds alloc] initWithRegion:visibleRegion];
CLLocationCoordinate2D northEast = coordinateBounds.northEast;
CLLocationCoordinate2D southWest = coordinateBounds.southWest;

Hope this helps.