Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a subview like UISlider or UIButtom in Google Maps in iPhone

I am new to iOS Programming and have very little knowledge about COCOA Touch. I am trying to add a button on the screen at the bottom over Google maps for giving an option to the user to go back to the previous screen. I just know that UIButton is a subclass of UIView and we can make button appear on a view by making it the sub view of that class. Previously iOS used to use Google Maps by default by in MKMapView and I have seen examples in books an on the Internet showing screen shots of apps where a button or a text box would appear on the map. But now just dragging the button in the interface builder doesn't help.

Text box over MKMapView

Here is my code:

ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>


@interface ViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UIButton *btn;


@end

ViewController.m

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController ()

@end

@implementation ViewController
{
    GMSMapView *mapView_;
}

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

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

- (void)loadView
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone;

    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    [locationManager startUpdatingLocation];

    //Latitude and longitude of the current location of the device.
    double lati = locationManager.location.coordinate.latitude;
    double longi = locationManager.location.coordinate.longitude;
    NSLog(@"Latitude = %f", lati);
    NSLog(@"Longitude = %f", longi);

    CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:lati longitude:longi];

    // Create a GMSCameraPosition that tells the map to display the coordinate

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lati
                                                            longitude:longi
                                                                 zoom:11.5];

    mapView_ = [GMSMapView mapWithFrame:[[UIScreen mainScreen] bounds] 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(lati, longi);
    marker.title = @"It's Me";
    marker.snippet = @"My Location";
    marker.map = mapView_;

    [mapView_ addSubview:_btn];
    [mapView_ bringSubviewToFront:_btn];

}

@end

Please let me know how it can be done.

Thanks.

like image 865
MK Singh Avatar asked May 08 '13 08:05

MK Singh


2 Answers

Do normal UI building on your current view, then add the GMSMapView as subview (at index 0) of the self.view (DON'T do self.view = mapView;)

Here is the significant code:

mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view insertSubview:mapView atIndex:0];

Inserting the map view at index 0 will set the rest of the objects in front.

like image 157
Raul Claus Avatar answered Sep 26 '22 01:09

Raul Claus


i suggest creating your button programmatically not using storyBoard, i tested this on "Google Maps SDK version: 1.4.0.4450" and it worked.

UIButton *button    = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame        = CGRectMake(10, 10, 100, 40);
[button setTitle:@"title" forState:UIControlStateNormal];

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.279526
                                                        longitude:-96.987305
                                                             zoom:2];

self.mapView    = [GMSMapView mapWithFrame:CGRectZero camera:camera];;
self.view       = self.mapView;

[self.view addSubview:button];
like image 45
Homam Avatar answered Sep 27 '22 01:09

Homam