9

I'm trying to put Google MapView in UIView but I get nothing displayed.

My code,

*.h

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

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIBarButtonItem *btnLocate;
@property (weak, nonatomic) IBOutlet GMSMapView *mapView_;
@property (weak, nonatomic) IBOutlet UIView *mapView;

@end

*.m

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6];
    self.mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.mapView_.myLocationEnabled = YES;
    self.mapView = self.mapView_;

Thanks.

Solution

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6];

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

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

So, i got the solution. Thanks anyway.

Best regards.

SamYan
  • 1,553
  • 1
  • 19
  • 38

2 Answers2

19

Solution:

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6];

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

    // Add as subview the mapview
    [self.mapView addSubview: self.mapView_]; 
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
SamYan
  • 1,553
  • 1
  • 19
  • 38
2

Essentially in the storyboard, for the corresponding View(UIVIew) you're going to show the Google Map, you have to set the class as GMSMapView in identity inspector.

This is how view controller would look like in Swift 3.

class GMapsViewController: UIViewController {
    @IBOutlet weak var mapContainerView: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)

       let marker = GMSMarker()
       marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
       marker.title = "Sydney"
       marker.map = mapContainerView

       mapContainerView.moveCamera(GMSCameraUpdate.setCamera(camera))


       }
}
Hashan Seneviratne
  • 1,157
  • 13
  • 24
  • Is it possible to bridge this to react native? – Jarrett Feb 26 '21 at 09:10
  • @Jarrett You should be able to but on react native there should be libraries already for this. – Hashan Seneviratne Feb 27 '21 at 05:23
  • @ Hashan Senevirathe Yeah i have heard of a library called `react-native-maps`, however it is not maintained anymore. So was just wondering if anyone have a simple example? Maybe not even for google maps but just to display a simple view. Any help would be appreciated! Thanks for the reply too! – Jarrett Feb 27 '21 at 05:48
  • @Jarrett Ah, I have used react-native-maps in a react-native project before. It is maintained and this project is active. Because I can see recent commits as well. Just that they are looking for folks to help maintain the project. So you can use this library easily. – Hashan Seneviratne Feb 27 '21 at 06:08