简体   繁体   English

在Google Maps iOS中重置/更新标记(swift3)

[英]Resetting/Updating Markers in Google Maps ios (swift3)

I am building an ios application that uses a google map to display the location of the places return in the server. 我正在构建一个使用google maps来显示服务器中返回地点的位置的ios应用程序。 I manage to display the result in the initial load of the map, but when I want to search new plaaces and reload the markers in the map. 我设法在地图的初始加载中显示结果,但是当我想搜索新地点并在地图中重新加载标记时。 Unfortunately the app was crashes. 不幸的是,该应用程序崩溃了。

Hope for your help. 希望对您有所帮助。

Thank you. 谢谢。

Here is my code in the ViewController to initially display the markers: 这是我在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

}

Here is my code to reload/reset the markers in the map. 这是我的代码,用于重新加载/重置地图中的标记。

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


}

Thanks for the help. 谢谢您的帮助。 Cheers! 干杯!

I found a solution, please see sample code below. 我找到了解决方案,请参见下面的示例代码。

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)
}

Thanks. 谢谢。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM