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
Have checked out and get working in Swift 2.0 all of the three options provided by cloudsurfin. Translating his answer for those who face the same problem in Swift 2.0:
Option1 - Put a view on the XIB and change its class to GMSMapView in XIB builder.
Option2 - Code it all. Add the map as a subview of the main view.
Option3 - Add the map as a GMSMapView subview of already existing UIView subview.
Explanation of preparations in XIB for Option1:
1) Set a class for your view:
2) Don't forget to connect your view with the outlet in code:
import UIKit
import GoogleMaps
class MapViewController: UIViewController {
@IBOutlet weak var map1 : GMSMapView!
@IBOutlet weak var map3 : UIView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.cameraWithLatitude(53.9,
longitude: 27.5667, zoom: 6)
// Option 1. view on XIB is class GSMMapView
self.map1.camera = camera;
// Option 2. add a map as a subview
let map2 = GMSMapView.mapWithFrame(CGRectMake(0, 64, 320, 460), camera:camera)
self.view.addSubview(map2)
// Option 3. add a map to a subview of UIView class that is already on the XIB
let map3 = GMSMapView.mapWithFrame(self.plainViewOnXIBHoldsMap3.bounds, camera:camera)
self.plainViewOnXIBHoldsMap3.addSubview(map3)
}
}
Hope this note helps someone :)
I spent about a half hour trying to get this to work and then I figured out that I had it in the wrong method. The google getting started example tells you to put it in - (void)loadView but if you do that you get a black screen or a full screen map. You need to put it in - (void)viewDidLoad. Then it works.
For Swift 3 and if you sub class the GMSMapView
UIView
into UIViewController
in story boardGMSMapView
instead UiView
Create reference in UIViewController
class
@IBOutlet weak var mapView: GMSMapView!
As you are subclassing you do not need use that GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mentioned in the docs. Just load map as follows
let camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0)
mapView.camera = camera
In order to add marker
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076)
marker.title = "London"
marker.snippet = "UK"
marker.map = mapView
Together as a function
override func viewDidLoad() {
super.viewDidLoad()
load()
}
func load() {
//map
let camera = GMSCameraPosition.camera(withLatitude: 51.50, longitude: -0.076, zoom: 10.0)
mapView.camera = camera
//marker
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 51.50, longitude: -0.076)
marker.title = "London"
marker.snippet = "UK"
marker.map = mapView
}
Based on other answers, here are three ways that actually work.
.h
@interface GPViewController : UIViewController
@property (strong, nonatomic) IBOutlet GMSMapView *map1;
@property (strong, nonatomic) IBOutlet UIView *plainViewOnXIBHoldsMap3;
@end
.m
- (void)viewDidLoad{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.0
longitude:151.20
zoom:6];
/* Option 1. view on XIB is class GSMMapView */
self.map1.camera = camera;
/* Option 2. add a map as a subview */
GMSMapView *map2 = [GMSMapView mapWithFrame:CGRectMake(0, 0, 100, 100) camera:camera];
[self.view addSubview:map2];
/* Option 3. add a map to a subview already on the XIB */
GMSMapView *map3 = [GMSMapView mapWithFrame:self.plainViewOnXIBHoldsMap3.bounds camera:camera];
[self.plainViewOnXIBHoldsMap addSubview:map3];
}