通过JSON显示动态GoogleMap标记

时间:2018-09-22 10:02:49

标签: swift google-maps cllocationmanager

从JSON获取纬度和经度响应后,如何在Google Map Marker中显示动态标记?这是我的显示标记代码,但是我在viewDidLoad(),因此无法正常工作,但如果未在viewDidLoad()中初始化相机对象,则相机对象会显示错误

这是showMarkerFunction:

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

    }

1 个答案:

答案 0 :(得分:0)

首先,将摄像机设置为类似的loadView方法

override func loadView() {

    // set mapView with settings
    let camera = GMSCameraPosition.camera(withLatitude: 30.101932, longitude: 31.379173, zoom: 15.5)
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    mapView.delegate = self
    mapView.isMyLocationEnabled = true
    mapView.settings.compassButton = true
    mapView.settings.myLocationButton = true
    mapView.setMinZoom(8, maxZoom: 20)
    self.mapView = mapView
    view = mapView
}

然后设置标记功能

func drawMarker(lat: CLLocationDegrees , long: CLLocationDegrees, description : String) {
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: lat, longitude: long)
    marker.icon = UIImage(named: "")
    marker.map = mapView
}

最后在viewDidLoad函数中调用您的方法

drawMarker(lat: Double("your value"), long: Double("your value"), description: "")