如何显示地址下面的谷歌地图,同时移动地图在swift



我在项目中使用谷歌地图。我只得到当前位置。但是我需要在标签中显示当前位置如果我移动地图那么标签地址也需要改变,怎么做呢

import UIKit
import GoogleMaps
import GooglePlaces
class ViewController: UIViewController {

@IBOutlet weak var belowaddressLabel: UILabel!
@IBOutlet weak var googleMapView: GMSMapView!

private var locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
}
extension ViewController: CLLocationManagerDelegate {
// 2
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
// 3
guard status == .authorizedWhenInUse else {
return
}

// 4
locationManager.startUpdatingLocation()
//5
googleMapView.isMyLocationEnabled = true
googleMapView.settings.myLocationButton = true
}
// 6
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
// 7
googleMapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
// 8
locationManager.stopUpdatingLocation()
}
}

这里我需要在belowaddressLabel中显示当前位置地址如何?请帮我写代码。

您可以使用CLGeocoder根据在locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])中传递的坐标获取地址。

// 7
CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in
guard let placemark = placemarks?.first else { return }
self.belowaddressLabel.text = placemark.name
})

请注意,您可以从placemark获取街道、城市等。参见CLPlacemark文档。

最新更新