【问题标题】:How to add custom markers from server to google map? Swift如何将自定义标记从服务器添加到谷歌地图?迅速
【发布时间】:2020-02-25 20:11:46
【问题描述】:

我正在开发应用程序,该应用程序会显示用户位置并加载附近的地点。地点是通过坐标从服务器获取的。所以我的问题是如何将它们显示为具有正确大小的自定义标记?我正在将坐标加载到数组中,然后在地图上显示它们。这是我已经走了多远:

标记在滚动时变大。

这是我设置标记的地方:

 let coordinates = spotCoordinates
let image = UIImage(named: "Dog")!.withRenderingMode(.alwaysTemplate)
for coord in coordinates {
   let position = CLLocationCoordinate2D(latitude: coord.latitude,longitude:coord.longitude)
    let marker = GMSMarker(position: position)
    marker.map = spotsParentView.mapView
    marker.icon = image
}

【问题讨论】:

    标签: ios swift google-maps google-maps-markers


    【解决方案1】:

    你必须像这样在数组中添加标记图标和位置:

     let marker = GMSMarkerInfo()
      marker.icon = UIImage(named: "locationPinNeon")
       self.markerArray.append(marker)                
    

    然后要在地图上从服务器获取的位置显示标记,您必须实现以下代码:

     let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: Double(self.lat)!, longitude: Double(self.long)!, zoom: 5.0)
                    self.mapView.camera = camera
    
                    delay(seconds: 1) { () -> () in
                        //fit map to markers
                        var bounds = GMSCoordinateBounds()
                        for marker in self.markerArray {
                            bounds = bounds.includingCoordinate(marker.position)
                        }
                        let update = GMSCameraUpdate.fit(bounds, withPadding: 100.0)
                        self.mapView.animate(with: update)
                    }
    

    【讨论】:

    • 嘿,谢谢您的回复,但我看不出它是如何回答我的问题的。 1. 我已经有图片了,我的目的是在地图上展示它们。 2. 你为 UIImageView 提供了一个扩展,但是在函数体中,你调用了类函数。如果我没有得到任何东西,请帮助我理解您的答案
    • 那么您是否使用谷歌聚类来显示附近的地方?
    • 我还没有添加谷歌集群。只需从服务器加载坐标,然后在地图上设置它们
    • 我已经编辑了答案,并根据它更新了您的代码。希望这对您有所帮助。
    • 我以几乎相同的方式做到了,您的回答对改进我的代码有很大帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多