在Google地图上显示多个“ GMSMarker”标记

时间:2018-12-02 07:32:41

标签: swift google-maps

我正在尝试使用Google地图显示多个位置,该代码仅显示一个位置。我正在使用for循环。我使用MKMapView检查了代码,并且可以正常工作。

代码如下:

let dict = [self.jsonElement]

            for dicts in dict {

                let latiCon = (dicts.value(forKey: "lati") as! NSString).doubleValue
                let longiCon = (dicts.value(forKey: "longi") as! NSString).doubleValue

            // Create a GMSCameraPosition that tells the map to display the
            // coordinate -33.86,151.20 at zoom level 6.
            let camera = GMSCameraPosition.camera(withLatitude: latiCon, longitude: longiCon, zoom: 6.0)
            let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
            self.view = mapView

            // Creates a marker in the center of the map.
            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: latiCon, longitude: longiCon)
            marker.title = "Sydney"
            marker.snippet = "Australia"
            marker.map = mapView

            }

我需要显示多个位置,我看过相对的答案,但找不到与我的问题相符的东西。

1 个答案:

答案 0 :(得分:0)

此代码

 let camera = GMSCameraPosition.camera(withLatitude: latiCon, longitude: longiCon, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        self.view = mapView

每次循环制作一个新的mapView,这很自然。 一个新的mapview +一个标记。 将此代码移到循环外,或者如果您之前已经声明过mapview,则将其删除。