如何将Google MapView放入UIView?

时间:2013-10-04 09:33:51

标签: ios uiview google-maps-sdk-ios

我试图将Google MapView放入UIView但我没有显示任何内容。

我的代码,

*的.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

*。米

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_;

感谢。

解决方案

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_]; 

所以,我得到了解决方案。不管怎样,谢谢。

最好的问候。

2 个答案:

答案 0 :(得分:17)

解决方案:

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_]; 

答案 1 :(得分:0)

基本上在故事板中,对于您要显示Google地图的相应视图(UIVIew),您必须在身份检查器中将类设置为GMSMapView

这是视图控制器在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))


       }
}