ios - Google Maps iOS Location 位于 MapView 的中心

标签 ios objective-c google-maps

我正在尝试在 subview 中将 Google map 加载到我的应用程序中。当我在 iOS 中初始化我的 GMSMapView 时,当前位置位于 map 的左上角而不是中央屏幕。如果我也按下“我的位置”按钮,情况也是如此。

这在我的手机上运行时似乎是个问题,但在模拟器上却不是。我如何获得它以便在中心正确设置它?

我尝试过重新排列代码并以不同方式加载 map 。我认为这可能是由于自动布局约束,因为当我设置 mapView.translatesAutoresizingMaskIntoConstraints = true; 然后位置在中心,但我的 View 被搞砸了。

我目前将 GMSMapView 设置为我的 View 的自定义类,并设置了自动布局约束以调整它的大小。 我加载我的 GPS 类并以此设置框架。

gpsVC = [[GPSViewController alloc] init];
gpsVC.view.frame=CGRectMake(0, CGRectGetMaxY(self.segmentTopView.frame), CGRectGetWidth(self.mainView.frame), CGRectGetHeight(self.mainView.frame)-CGRectGetHeight(self.topView.frame)-CGRectGetMaxY(self.segmentTopView.frame));
[self.segmentView addSubview:gpsVC.view];

在我的 GPSViewController 中,我按如下方式设置了 map 相机:

self.mapView.myLocationEnabled = YES;
GMSCameraPosition *camera=[GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude longitude:myLocation.coordinate.longitude zoom:[mapDefaultZoomLevel floatValue]];
self.mapView.camera=camera;

还有其他人遇到过这种情况吗?您是否找到了解决此问题的方法?

编辑:这里有一些图片(链接,因为我还不能放入图片)

https://imgur.com/FEMfwri
https://imgur.com/ySQio5b
https://imgur.com/9kijxbT

最佳答案

对于任何想知道我最终找到了解决我遇到的问题的解决方案的人。

我认为在我的 GPSViewController 初始化时发生了自动布局的 View 框架问题。我在设置 View 后通过设置 Google map 避免了这个问题。我从 UIView 的自定义类中删除了 GMSMapView,而是以编程方式手动创建 map 并将其添加到 View 中。

在我的 MasterViewController.m 中:

gpsVC = [[GPSViewController alloc] init];
gpsVC.view.frame=CGRectMake(0, CGRectGetMaxY(self.segmentTopView.frame), CGRectGetWidth(self.segmentView.frame), CGRectGetHeight(self.segmentView.frame)-CGRectGetHeight(self.segmentTopView.frame));
[gpsVC setupMap];

在我的 GPSViewController.m(删节)中:

-(void)setupMap{
   GMSCameraPosition *camera=[GMSCameraPosition cameraWithLatitude:coordinate.latitude longitude:coordinate.longitude zoom:mapDefaultZoomLevel];
   self.mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) camera:camera];
   self.mapView.myLocationEnabled = YES;
   self.mapView.settings.myLocationButton = YES;
   self.mapView.settings.compassButton = YES;
   self.mapView.delegate=self;
   [self.myView addSubview:self.mapView];
}

关于ios - Google Maps iOS Location 位于 MapView 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56670328/

相关文章:

javascript - 使用前端路由防止 iOS chrome 状态栏重新出现

ios - 如何水平定位 UINavigationBar 中的 backIndicatorImage

iphone - 如何在我的本地应用程序中集成 Skype 功能

ios - 如何在谷歌地图中显示集群时隐藏标记

jquery - 如何在谷歌地图上触发places_changed而无需手动输入

iphone - 使用自定义 NSObject 类来简化 iPhone 应用程序中的重复代码时遇到问题

iphone - 修改 UITableView 的侧边栏?

objective-c - 从不兼容类型 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' 分配给 'CameraVIewController*' '

广告商和浏览器的 iOS Multipeer 连接

android - 使用 Googlemaps API 将用户引导至某个位置 (Android)