简体   繁体   中英

GMSMarker icon in the top left corner of the view (iOS)

I am trying to create a UITableViewCell containing a GMSMapView with a GMSMarker at the center of the current Position.

The problem is that the marker always appears at the top left corner of the current position and I don't know how to solve the problem.

I tried to follow these steps: Implementing a Google Map with UItableviewCell

here is my code from cellForRowAt:

let locationCell = tableView.dequeueReusableCell(withIdentifier: "activityLocationCell") as! ActivityLocationCell

let latitude = CLLocationDegrees(activity.coordinates![0])
let longitude = CLLocationDegrees(activity.coordinates![1])
let position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

locationCell.googleMapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 15)

let marker = GMSMarker(position: position)
marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
marker.map = locationCell.googleMapView

return locationCell

Here is a screenshot of my problem: marker is at the top left corner of the map

I had a pretty similar issue. I resolved it by changing the moment I configure the map in the view lifecycle.

In my case, I was using a child view controller. I was configuring the map before viewWillAppear was called which caused the map to not center properly (the marker was on the top left corner). I moved my call to after the viewWillAppear and it fixed it. A good place would be viewDidAppear .

If you are using a cell, you will probably need to investigate with the view lifecycle instead of the controller lifecycle.

This is not written anywhere on the Google documentation.

you have to draw map in func viewDidLayoutSubviews()

Try creating Marker when map is ready completely. for eg: use the delegate.

  var ifMapReady: Bool = false 
  ...
  ...
  func mapViewSnapshotReady(_ mapView: GMSMapView) {
        ifMapReady = true
  }
  //Call this method from where ever you want to load map
  func updateMap() {
       if ifMapReady {
            //Load Map
       }
  }

This delegate will be called multiple times(eg: map is swiped or moved etc) whenever the Map tiles are ready. So we can use a boolean value for understanding that map loaded successfully. Based on that value we can load the Map properly when initiating.

I want to add one more thing. @Gabriel Cartier's answer worked for me with one additional change in my code.

[self->mapView_ animateToCameraPosition:camera];  

And I replaced with

[self->mapView_ setCamera:camera];

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