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

流过昼夜 提交于 2020-01-01 05:47:05

问题


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


回答1:


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.




回答2:


you have to draw map in func viewDidLayoutSubviews()



来源:https://stackoverflow.com/questions/44989913/gmsmarker-icon-in-the-top-left-corner-of-the-view-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!