Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 339
steed Avatar asked Jul 08 '17 19:07

steed


2 Answers

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.

like image 85
Gabriel Cartier Avatar answered Sep 17 '22 14:09

Gabriel Cartier


you have to draw map in func viewDidLayoutSubviews()

like image 28
Chiman Song Avatar answered Sep 18 '22 14:09

Chiman Song