【问题标题】:iOS Google maps sdk - markers are not pointed while using subview instead of main viewiOS Google maps sdk - 使用子视图而不是主视图时未指向标记
【发布时间】:2014-08-17 19:00:19
【问题描述】:

尝试在“位置”位置显示带有标记的简单 Google 地图。

如果 mapview_ 显示在 self.view(控制器的默认视图)而不是 GMSMapView 类的 gmapsView(self.view 的子视图)中,则一切正常。

我浏览了 SO 上的一些帖子,但无法解决问题。 在 Interface builder gmapsview 类中设置为 GMSMapView。

相机位置设置

 CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
                                                             37.778376,
                                                             -122.409853);
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:position.latitude longitude:position.longitude];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:position.coordinate.latitude
                                                        longitude:position.coordinate.longitude
                                                             zoom:13];  

地图设置

mapView_ = [GMSMapView mapWithFrame:self.gmapsView.frame camera:camera];
mapView_.delegate = self;
self.gmapsView.camera = camera; //gmapsView is a view of class GMSMapView

CGRect rect = self.locationBlockView.frame;

self.gmapsView.delegate = self; //Added to check whether it works
self.gmapsView = mapView_;  

设置标记

GMSMarker *marker = [GMSMarker markerWithPosition:position]; //Adding Marker
marker.map = mapView_;
[mapView_ animateToLocation:position.coordinate];

【问题讨论】:

  • 我不明白你我害怕。你想要什么,什么有效,什么无效?
  • @Daij-Djan 1. 地图和标记正在默认视图中加载。 2.地图加载在子视图中,但不显示标记。
  • gmapsView 属于“GMSMapView”类。类在 IB 中设置并在控制器中有链接。
  • 那你为什么要分配初始化一个新实例?
  • 是的,这就是错误!最初尝试由 developer.google 提供的示例代码,后来根据我的需要进行了修改。我刚刚尝试删除实例化并从中提琴它的工作。但从逻辑上讲,即使重新实例化它也应该可以正常工作?在正常情况下,当通过重新初始化访问 UIView 时,它为什么会在这里失败?

标签: ios objective-c google-maps ios7 google-maps-sdk-ios


【解决方案1】:

重新初始化地图是导致问题的原因。一旦它被删除,它就解决了这个问题。但是重新初始化 GMSMapView 会导致问题吗?这是一个荒谬的事情,但它解决了这个问题。

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
                                                             12.778376,
                                                             -122.409853);
currentLocation = [[CLLocation alloc] initWithLatitude:position.latitude longitude:position.longitude];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:eventLocation.coordinate.latitude
                                                        longitude:eventLocation.coordinate.longitude
                                                             zoom:13];
//[self.gmapsView removeFromSuperview];//removing the subview
//self.gmapsView = [GMSMapView mapWithFrame:self.gmapsView.frame camera:camera];
//self.gmapsView.delegate = self;

//self.gmapsView = self.gmapsView; //set map

// [self.view addSubview:self.gmapsView];//Adding back did the magic
self.gmapsView.delegate = self; //set delegate
self.gmapsView.camera = camera; //set camera

GMSMarker *marker = [GMSMarker markerWithPosition:position]; //Adding Marker
marker.map = self.gmapsView;
[self.gmapsView animateToLocation:currentLocation.coordinate];

【讨论】:

    【解决方案2】:

    将 Google 地图添加到子视图

    解决步骤

    1) 将故事中 UIView 的类更改为 GMSMapVIew。

    2) 接下来我为绿色的 UIView 容器创建了一个 IBOutlet 并连接了它。

    #import <UIKit/UIKit.h>
    #import <GoogleMaps/GoogleMaps.h>
    
    @interface CarLocationViewController : UIViewController  
    //An outlet of type GMSMapView
    @property (strong, nonatomic) IBOutlet GMSMapView *mapContainerView;
    @end
    

    3) 然后在我看来确实加载了我做了以下操作

    - (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;
    
    }
    

    我正在关注此链接http://www.ryanwright.me/cookbook/ios/obj-c/maps/gmap/subview

    【讨论】:

    • 它现在是一年多前的问题,一年前得到了回答,您在第 1 步和第 2 步中所做的事情在问题下方进行了评论。您是否从链接中总结了整个教程?一个想法,有一个错误,纠正回答它,我觉得指出错误应该是一个答案!
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 2015-09-26
    • 2017-03-05
    相关资源
    最近更新 更多