【问题标题】:Google Maps SDK not displaying properly in UIView?Google Maps SDK 无法在 UIView 中正确显示?
【发布时间】:2014-11-06 01:58:31
【问题描述】:

我在 Xcode 6.1 中显示谷歌地图时遇到了一些问题

我设法让地图显示在我的 UIView 的一部分中,但它显示的是世界地图而不是特定坐标。

问题来了:

.h:

@interface MapViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIView *googleMapView;

- (IBAction)mapToMain:(UIButton *)sender;

@end

.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134 
    longitude: -117.140269 zoom: 17];

    GMSMapView *mapView = [GMSMapView mapWithFrame:self.googleMapView.bounds
    camera:camera];
    mapView.myLocationEnabled = YES;

    self.googleMapView = mapView;

    GMSMarker *marker = [ [GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
    marker.title = @"Tet Festival 2015";
    marker.snippet = @"VAYA Tet";
    marker.map = mapView;
}

我在这里看到了一些建议,但没有奏效:

Cannot put a google maps GMSMapView in a subview of main main view?

Using the Google Maps SDK in views other than the main view

有什么建议吗?

【问题讨论】:

    标签: ios objective-c xcode xcode6


    【解决方案1】:

    您必须在 Storyboard 中添加一个 UIView...但之后您必须将这个简单的 UIView 变成一个 GMSMapView!选择刚刚添加的视图,在 Utilities 工具栏中选择左起第三个选项卡打开 Identity Inspector,将视图的类名从 UIView 更改为 GMSMapView,如下图所示:

    你的奥特莱斯不会是这样的

    @property (strong, nonatomic) IBOutlet UIView *googleMapView;
    

    会是这样的

    @property (strong, nonatomic) IBOutlet GMSMapView *mapView;
    

    你的 ViewDidLoad 可以是这样的:

     GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134
                                                                longitude: -117.140269 zoom: 17];
    
        [self.mapView animateToCameraPosition:camera];
    
        GMSMarker *marker = [ [GMSMarker alloc] init];
        marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
        marker.title = @"Tet Festival 2015";
        marker.snippet = @"VAYA Tet";
        marker.map = self.mapView;
    

    更新

    Here you have an example project(插入您的密钥)

    【讨论】:

    • 谢谢!存在和 Swift 相同的问题。必须需要更改 UIView 的自定义类
    【解决方案2】:

    终于明白了……

    已删除:

    self.googleMapView = mapView;
    

    替换:

    [self.googleMapView addSubview:mapView];
    

    【讨论】:

      【解决方案3】:

      我不确定上面的代码有什么问题。对我来说看起来不错。但是要将相机移动到某个位置。您也可以在那里为相机设置动画。

      var newLocation = CLLocationCoordinate2DMake(latitude, longitude)
      googleMapView.animateToLocation(newLocation)
      

      希望它对你有用。

      【讨论】:

      • 我基本上是想让地图显示标记位置,但它显示的是世界地图。
      • 其他人可以帮忙吗?我搜索了所以仍然无法让它工作:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 1970-01-01
      相关资源
      最近更新 更多