[英]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.我正在尝试创建一个包含 GMSMapView 的 UITableViewCell,在当前位置的中心有一个 GMSMarker。
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我尝试按照以下步骤操作: Implementing a Google Map with UItableviewCell
here is my code from cellForRowAt:这是我来自 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).我在调用viewWillAppear
之前配置地图,这导致地图无法正确居中(标记位于左上角)。 I moved my call to after the viewWillAppear
and it fixed it.我将呼叫移至viewWillAppear
之后并修复了它。 A good place would be viewDidAppear
.一个好地方是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()你必须在 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. @Gabriel Cartier 的回答对我有用,我的代码做了一个额外的更改。
[self->mapView_ animateToCameraPosition:camera];
And I replaced with我换成了
[self->mapView_ setCamera:camera];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.