【问题标题】:Resetting/Updating Markers in Google Maps ios (swift3)在 Google Maps ios (swift3) 中重置/更新标记
【发布时间】:2018-01-19 09:13:44
【问题描述】:

我正在构建一个 ios 应用程序,它使用谷歌地图来显示服务器中返回的地点的位置。我设法在地图的初始加载中显示结果,但是当我想搜索新的地方并重新加载地图中的标记时。不幸的是,该应用程序崩溃了。

希望得到您的帮助。

谢谢。

这是我在 ViewController 中最初显示标记的代码:

func setUpMap() {

    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
    self.mapView.camera = camera
    let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

    for data in arrData {

        let long = data["LONGITUDE"].string!
        let lat = data["LATITUDE"].string!

        if long != "" || lat != "" {

            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
            marker.title = ""
            marker.snippet = "Hey, this is sample"
            marker.icon = UIImage(named: "locator")
            marker.map = self.mapView

            bounds = bounds.includingCoordinate(marker.position)

        }

    }

    // fit to bounds
    mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))

    self.mapView = map_view

}

这是我重新加载/重置地图中标记的代码。

func reloadMarkers() {

    self.mapView.clear()

    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
    self.mapView.camera = camera
    let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)


    var _bounds = GMSCoordinateBounds()
    for data in arrData {

        let long = data["LONGITUDE"].string!
        let lat = data["LATITUDE"].string!

        if long != "" || lat != "" {

            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
            marker.title = ""
            marker.snippet = "Hey, this is sample"
            marker.icon = UIImage(named: "locator")
            marker.map = self.mapView

            _bounds = _bounds.includingCoordinate(marker.position)

        }

    }

    // fit to bounds
    mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))

    self.mapView = map_view


}

感谢您的帮助。 干杯!

【问题讨论】:

  • 你能发布你的崩溃日志吗?
  • 当我在我的 reloadMarkers 方法中设置 GMSCameraPosition 时,它只是在行中显示“错误:在展开可选值时意外发现 nil”
  • 然后你找到了答案,你的问题是latlong不能转换为CLLocationDegrees请打印什么经纬度包含
  • 你好,它包含有效的坐标,我还添加了一些检查来检查纬度或经度是否为空。
  • 你能把你的应用程序崩溃的那一行贴出来吗?

标签: ios google-maps google-maps-api-3 swift3


【解决方案1】:

我找到了解决办法,请看下面的示例代码。

func reloadMarkers() {

    let camera = GMSCameraPosition.camera(withLatitude: 12.0377995, longitude: 122.6408281, zoom: 10.0)
    let map_view = GMSMapView.map(withFrame: self.brachMapView.bounds, camera: camera)
    map_view.camera = camera

    var bounds = GMSCoordinateBounds()

    for data in arrData {

        let long = data["LONGITUDE"].string!
        let lat = data["LATITUDE"].string!

        if long != "" || lat != "" {

            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
            marker.title = ""
            marker.snippet = "Hey, this is sample"
            marker.icon = UIImage(named: "locator")
            marker.map = map_view

            bounds = bounds.includingCoordinate(marker.position)

        }

    }

    // fit to bounds
    map_view.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))

    self.brachMapView.addSubview(map_view)
}

谢谢。

干杯

【讨论】:

    猜你喜欢
    • 2012-01-28
    • 2013-12-28
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2017-06-12
    • 2012-03-26
    • 1970-01-01
    相关资源
    最近更新 更多