简体   繁体   中英

Google Maps iOS Location is centered in corner of MapView

I am trying to load a Google Map into my application within a subview. When I initialize my GMSMapView in iOS, the current location is in the top left corner of the map instead of the center screen. This is true if I were to press the "My location" button as well.

This seems to be an issue when running on my phones but not on simulators. How do I get it so that it is set up correctly in the center?

I've tried rearranging code and loading the map differently. I think it might be due to Autolayout Constraints, as when I set mapView.translatesAutoresizingMaskIntoConstraints = true; then the location is in the center, but my view is messed up.

I currently have GMSMapView set up as the custom class of my view with auto layout constraints set up to resize it. I load up my GPS class and set the frame with this.

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

And in my GPSViewController I set up the map camera as follows:

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

Is anyone else experiencing this, and did you find any solution to this problem?

Edit: Here are some images (Links because I can't put in images yet)

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

for anyone wondering I eventually found a workaround solution to the problem I was having.

I believe that there was a view frame issue with auto-layout that was happening when my GPSViewController initialized. I avoided this issue by setting up my Google Map after my view has been setup. I removed the GMSMapView from the custom class of my UIView and instead programmatically created the map manually and added it to the view.

In my 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];

In my GPSViewController.m (abridged):

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

Looks like you are doing it correctly, the only thing I can't say for sure is whether you are centering your map correctly in the sub view. But here are some other map tricks you can try, if the problem is with the map:

   [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]];
    }

Same issue with you. I was delay init google mapview to test. And It works normally again.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM