简体   繁体   English

视图左上角的 GMSMarker 图标 (iOS)

[英]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.

相关问题 拖动视图在iOS7中有效,在iOS8中,每次都会将视图重置为左上角 - Drag view works in iOS7, in iOS8 it resets view to top left corner everytime 中心的 GMSMarker 图标 (iOS) - GMSMarker icon from center (iOS) 拐角半径仅适用于视图的左上角和左下角 - Corner radius only for top and bottom left corner of a view 如何在Google Maps for iOS中调整GMSMarker图标的大小? - How to resize a GMSMarker icon in Google Maps for iOS? 我们可以在 iPhone 应用程序图标的最左上角显示徽章图标,而不是通常的最右上角显示吗? - Can we show badge icon on left top most corner of an app icon in iPhone instead of usual right-top-most corner display? 如何在 iOS 中的 UICollectionViewCell 的左上角添加按钮? - How to add button to top left corner in UICollectionViewCell in iOS? iOS9弹出框始终指向锚点的左上角 - iOS9 popover always points to top-left corner of anchor 如何在ios中将图像添加到uicollectionView单元的左上角? - How to add the image to top-left corner of uicollectionView cell in ios? 在ios Emulator / IPhone5中,在左上角设置UIView损坏了吗? - Setting UIView on the left top corner is broken in ios Emulator/IPhone5? 如何在iOS中相对于左上角旋转UILabel - How to rotate UILabel with respect to Top Left corner in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM