Ios i';我在地图视图中添加多个标记时遇到问题

Ios i';我在地图视图中添加多个标记时遇到问题,ios,swift4,google-maps-markers,google-maps-sdk-ios,Ios,Swift4,Google Maps Markers,Google Maps Sdk Ios,我在地图视图中添加多个标记时遇到问题,但我总是显示一个标记,这是最后一个被调用的标记,我不知道为什么。 其目的是获取包含纬度和经度的数据,并添加这些标记。我正试图静态地进行此操作,但我可以显示多个标记 我创建了一个添加新标记的函数,并在viewDidLoad中调用它 `重写func viewDidLoad(){ super.viewDidLoad() `在addMarker方法之外创建一个GMSMapView实例作为实例属性。并在addMarker方法中更改其相机位置并添加标记 let mapV

我在地图视图中添加多个标记时遇到问题,但我总是显示一个标记,这是最后一个被调用的标记,我不知道为什么。 其目的是获取包含纬度和经度的数据,并添加这些标记。我正试图静态地进行此操作,但我可以显示多个标记

我创建了一个添加新标记的函数,并在viewDidLoad中调用它 `重写func viewDidLoad(){ super.viewDidLoad()


`

在addMarker方法之外创建一个GMSMapView实例作为实例属性。并在addMarker方法中更改其相机位置并添加标记

let mapView = GMSMapView()

private func addMarker(title:String, snippet:String , latitude:Double , longitude:Double){
    let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 6.0)
    self.mapView.animate(to: camera)

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    marker.title = title
    marker.snippet = snippet
    marker.map = mapView
}
逐个添加多个标记时,不要将相机位置设置为最后一个标记位置。要在地图视图中显示所有标记,可以使用
GMSCoordinateBounds

let mapView = GMSMapView()
var bounds = GMSCoordinateBounds()

override func viewDidLoad() {
    super.viewDidLoad()
    addMarker(title: "pala", snippet: "nanana", latitude: 35.741522, longitude: 9.805937)
    addMarker(title: "pala", snippet: "nanana", latitude: 36.89939467218524, longitude: 10.187976658321267)
}
private func addMarker(title:String, snippet:String , latitude:Double , longitude:Double){
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    marker.title = title
    marker.snippet = snippet
    marker.map = mapView

    bounds = bounds.includingCoordinate(marker.position)
    let update = GMSCameraUpdate.fit(bounds, withPadding: 50)
    mapView.animate(with: update)
}
let mapView = GMSMapView()
var bounds = GMSCoordinateBounds()

override func viewDidLoad() {
    super.viewDidLoad()
    addMarker(title: "pala", snippet: "nanana", latitude: 35.741522, longitude: 9.805937)
    addMarker(title: "pala", snippet: "nanana", latitude: 36.89939467218524, longitude: 10.187976658321267)
}
private func addMarker(title:String, snippet:String , latitude:Double , longitude:Double){
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    marker.title = title
    marker.snippet = snippet
    marker.map = mapView

    bounds = bounds.includingCoordinate(marker.position)
    let update = GMSCameraUpdate.fit(bounds, withPadding: 50)
    mapView.animate(with: update)
}