显示来自 JSON 的动态谷歌地图标记



JSON 获取纬度和经度响应后,我如何在 Google 地图标记中显示动态标记,这是我的显示标记代码,但我在 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
}

首先,像这样在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
}

最后在视图中DidLoad 函数调用您的方法

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

相关内容

  • 没有找到相关文章

最新更新