谷歌地图iOS定位在MapView的角落里

3
我想在子视图中将Google地图加载到我的应用程序中。在iOS中初始化GMSMapView时,当前位置位于地图的左上角而不是居中。如果我按“我的位置”按钮,情况也是如此。这个问题似乎只存在于我的手机上而不是模拟器上。如何使其正确设置为中心呢?
我尝试过重新排列代码和以不同方式加载地图。我认为这可能是由于自动布局约束导致的,因为当我设置mapView.translatesAutoresizingMaskIntoConstraints = true;时,位置就在中心,但我的视图就会出错。
我目前将GMSMapView设置为我的视图的自定义类,并设置好自动布局约束以调整大小。我使用以下代码载入我的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中,我按照以下方式设置地图摄像机:
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
3个回答

3

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

我认为我的GPSViewController初始化时出现了自动布局的视图框架问题。我通过在视图设置完成后设置Google地图来避免出现此问题。我从我的UIView的自定义类中删除了GMSMapView,并手动编程创建了地图并将其添加到视图中。

在我的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];
}

0

看起来你做得很正确,唯一我不能确定的是你是否在子视图中正确居中地图。但如果问题出在地图上,这里有一些其他的地图技巧可以尝试:

   [self.mapView clear];

    // Set map view and display
    double zoom = 14;

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[myLocation.latitude doubleValue]
                                                            longitude:[myLocation.longitude doubleValue]
                                                                 zoom:zoom];

    // Initialize Map View
    self.mapView.camera = camera;
    self.mapView.myLocationEnabled = YES;

    __block GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];

   //You can try setting a couple of points east and west of your current location and including them in the bounds


    CLLocation westLocation = myLocation;
    westLocation.longitude += .005; 
    CLLocation eastLocation = myLocation;
    eastLocation.longitude -= .005;       

    bounds = [bounds includingCoordinate:myLocation.position];
    bounds = [bounds includingCoordinate:eastLocation.position];
    bounds = [bounds includingCoordinate:westLocation.position];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:40.0f]];
    } else {
        [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:100.0f]];
    }

嘿,Michael,谢谢你的回复。我尝试了你的代码,但好像没有什么区别。我应该指出这似乎是我的iPhone Xs设备的问题,但在模拟器上它似乎可以正常工作。我正在尝试很多方法,但还是找不到解决方案。 - AtomicDragon
你确认地图填充了子视图,而子视图填充了其父视图吗?只是想确定问题是出在视图上还是具体地图渲染上。 - Michael Dougan
谢谢Michael,经过寻找和调整我的子视图框架,我想我找到了一个解决方案。 我的想法是,当Google地图首次初始化时,它的框架为0x0,这使得中心点位于0,0标记处。我认为这是一个框架和自动布局问题。我通过在所有框架和视图布局设置完成后创建我的地图来解决了这个问题,而不是在地图渲染时创建。 - AtomicDragon
听起来就是正在发生的事情。记住这一点很重要!谢谢! - Michael Dougan

0

我和你遇到了同样的问题。我延迟了初始化谷歌地图视图以进行测试。然后它又正常工作了。

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [self initGoogleMap];
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接