首页 > 解决方案 > 从 Rest API 获得响应后添加动态 GoogleMap Marker

问题描述

从服务器获取坐标后如何做动态谷歌标记我从 REST API 获取纬度和经度

我的showMarker函数如下:

private func showMarker(_ lat: String, _ lng: String, _ description: String){

        let latitude: CLLocationDegrees = Double(lat) ?? 0.00
        let longitude: CLLocationDegrees = Double(lng) ?? 0.00

        print("Latitude: \(latitude), Longitude: \(longitude)")


        let position = CLLocationCoordinate2DMake(latitude, longitude)
        let marker = GMSMarker(position: position)
        marker.title = ""
        marker.map = self.mapView

    }

如果下面的代码在 ViewDidLoad() 中运行正常

    self.camera = GMSCameraPosition.camera(withLatitude: 6.244880, longitude: 7.001920, zoom: 10.0)
    mapView.camera = self.camera
    mapView.animate(toZoom: 10.0)

    self.mapView = GMSMapView.map(withFrame: self.view.bounds, camera: self.camera)

但是当我在上面的代码之后在 showMarker 函数之后调用它们时,相机对象得到 nil

我只想在通过rest api获取数据后添加标记

标签: swiftxcodegoogle-mapsgoogle-maps-markers

解决方案


推荐阅读