ios - 左上角问题中的 Swiftui Google Maps 用户标记
问题描述
当我打开地图时,用户的标记在左上角,而不是在中心,只是当我点击显示我的位置按钮时,相机变成了用户标记再次在左上角。问题是什么 ?如何在地图中心显示用户标记。
我的 LocationManager 类:
class LocationManager: NSObject, ObservableObject {
// Singleton instance
public static let shared : LocationManager = {
return LocationManager()
}()
private let locationManager = CLLocationManager()
@Published var location: CLLocation = CLLocation(latitude: 0.0, longitude: 0.0)
override init() {
super.init()
locationManager.delegate = self
locationManager.allowsBackgroundLocationUpdates = true
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.requestWhenInUseAuthorization()
location = locationManager.location ?? CLLocation(latitude: 0.0, longitude: 0.0)
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let currentLocation = locations.last else { return }
self.location = currentLocation
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.requestLocation()
location = locationManager.location ?? CLLocation(latitude: 0.0, longitude: 0.0)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {
appLog.debug(error)
}
}
地图视图:
struct GoogleMapView: UIViewRepresentable {
@Binding var coordinates: [Coordinate]
let zoom: Float = 15.0
var showMyLocationButton = true
var mapBoundsCallback: ([Double], [Double]) -> ()
private var cancellables = Set<AnyCancellable>()
private var mapView: GMSMapView?
init(coordinates: Binding<[Coordinate]>,
zoom: Float = 15.0,
showMyLocationButton: Bool = true,
mapBoundsCallback: @escaping ([Double], [Double]) -> ()) {
self.showMyLocationButton = showMyLocationButton
self.mapBoundsCallback = mapBoundsCallback
_coordinates = coordinates
}
func makeUIView(context: Self.Context) -> GMSMapView {
let camera = GMSCameraPosition.camera(withTarget: LocationManager.shared.location.coordinate , zoom: zoom)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.animate(to: camera)
mapView.delegate = context.coordinator
mapView.settings.compassButton = true
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = showMyLocationButton
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: Context) {
mapView.clear()
if coordinates.isEmpty {
return
}
let path = GMSMutablePath()
var bounds = GMSCoordinateBounds()
for coordinate in coordinates {
if let lat = coordinate.coordinates?[0], let lng = coordinate.coordinates?[1] {
let locationCoordinate = CLLocationCoordinate2DMake(lat, lng)
path.add(locationCoordinate)
bounds = bounds.includingCoordinate(locationCoordinate)
}
}
let rectangle = GMSPolyline(path: path)
rectangle.strokeColor = Theme.colorDarkGrey.uiColor()
rectangle.strokeWidth = 2.0
rectangle.map = mapView
mapView.animate(with: GMSCameraUpdate.fit(bounds))
}
func makeCoordinator() -> Coordinator {
Coordinator { ne, sw in
mapBoundsCallback(ne, sw)
}
}
class Coordinator: NSObject, GMSMapViewDelegate {
var cameraPositionCallback: ([Double], [Double]) -> ()
init(cameraPositionCallback: @escaping ([Double], [Double]) -> ()) {
self.cameraPositionCallback = cameraPositionCallback
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let farRight = mapView.projection.visibleRegion().farRight
let nearLeft = mapView.projection.visibleRegion().nearLeft
let ne: [Double] = [Double(farRight.latitude), Double(farRight.longitude)]
let sw: [Double] = [Double(nearLeft.latitude), Double(nearLeft.longitude)]
cameraPositionCallback(ne, sw)
}
}
}
解决方案
推荐阅读
- twilio - 从谷歌电子表格数据库向大众发送个性化的 Whatsapp 消息
- c# - 从后缀列表中获取主要单词
- android - 错误:卸载后具有相同包但签名不同的应用程序
- python - Flask SQLAlchemy 批量删除记录
- javascript - 如何在highcharts js中显示负值?
- c - 将数组传递给函数而不是变量
- javascript - DISCORD.JS TypeError:无法读取未定义的属性“url”
- python - 在 numpy 中生成大小为 n、均值 = 20、min=2 和 max=25 的整数列表
- reactjs - ReactJS 调用 API 和重定向
- postgresql - 无法在 docker compose 中访问 Postgres 容器