I\'m struggling with this problem!
I want to add a google maps GMSMapView
into a UIView
that is only a portion of the main UIView
of m
Following the google tutorial for Swift in Sep-2017
I fixed by putting the code in viewDidLoad like this
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Create a GMSCameraPosition to display the initial center of the map
let camera = GMSCameraPosition.camera(withLatitude: 23.885942, longitude: 45.079162, zoom: 5.1)
// Set the frame size , don't forget to replace "<CustomFrame>" with the required frame size
mapView = GMSMapView.map(withFrame: "<CustomFrame>", camera: camera)
// Add the map to any view here
view.addSubview(mapView) //or customView.addSubview(mapView)
}
I was getting the black screen just because i forgot to add this line:
[super loadView];
(void)viewDidLoad {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:14];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.map = self.mapContainerView;
//set the camera for the map
self.mapContainerView.camera = camera;
self.mapContainerView.myLocationEnabled = YES;
}
My suggestions:
.h
@interface MapViewController: UIViewController <CLLocationManagerDelegate>
{
IBOutlet UIView *mapViewOnSreen;
}
.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:10];
_mapView = [GMSMapView mapWithFrame:mapViewOnScreen.bounds camera:camera];
_mapView.myLocationEnabled = YES;
mapViewOnScreen = _mapView
//OR [self.view addSubview:_mapView];
//OR [mapViewOnScreen addSubview:_mapView]; <--This worked for me
}
*Edit: I created a small UIView inside the main view using IB. I followed the steps listed and I was able to view the map inside the small UIView when I set [mapViewOnScreen addSubview:_mapView];
you can do it through IB
.h
@property (weak, nonatomic) IBOutlet GMSMapView *theMapView;
.m
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:12];
//This next line is not needed if you have a GMSMapView outlet
//self.theMapView = [GMSMapView mapWithFrame:self.theMapView.frame camera:camera];
self.theMapView.camera=camera;
self.theMapView.mapType = kGMSTypeSatellite;
self.theMapView.delegate=self;
set the UIVIEW class to be GMSMapView in the identity inspector.
then make an IBOutlet from it :D and hola it is work :P