首页 > 解决方案 > 如何使用 SwiftUI 和 GoogleMaps iOS SDK 关注用户位置

问题描述

我对 Xcode 和 SwiftUI 还是新手,所以如果这对某些人来说很简单,我很抱歉。从本质上讲,我正在制作一个使用 GoogleMaps iOS SDK 的应用程序,并且我试图弄清楚如何在启动时将地图的相机聚焦到用户的当前位置。

现在我的代码的工作方式是我有一个调用 GoogleMaps 视图和其他几个视图的主视图。地图初始化并且地图的相机聚焦在我定义的设定位置(波士顿),在特定位置(波士顿)放置一个大头针,它用蓝点显示我的当前位置,并且相机仅在点击 myLocation 按钮(这是谷歌地图固有的)。

我调用我的谷歌地图视图的主要视图在这里:


import SwiftUI

struct LandmarkList: View {

    @State private var searchText = ""
    @State private var locationText = ""

    var body: some View {

        NavigationView {

            GoogMapView()
                .frame(height: 750)
        }
        .edgesIgnoringSafeArea(.vertical)
        .navigationBarItems(leading:

        TextField("Current Location",text: self.$locationText)
        .textFieldStyle(RoundedBorderTextFieldStyle())
        .multilineTextAlignment(.leading)
        .frame(width: 375)
            )

        }

我的 Google 地图视图(我称之为 GoogMapView):根据以下评论和教程更新:

import SwiftUI
//import MapKit
import UIKit
import GoogleMaps
import GooglePlaces


private let locationManager = CLLocationManager()

struct GoogMapView : UIViewRepresentable {

        let marker : GMSMarker = GMSMarker()

        //Creates a `UIView` instance to be presented.
        func makeUIView(context: Self.Context) -> GMSMapView {
            // Create a GMSCameraPosition
            let camera = GMSCameraPosition.camera(withLatitude: 42.361145, longitude: -71.057083, zoom: 16.0)
            let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
            mapView.setMinZoom(14, maxZoom: 20)
            mapView.settings.compassButton = true
            mapView.isMyLocationEnabled = true
            mapView.settings.myLocationButton = true
            mapView.settings.scrollGestures = true
            mapView.settings.zoomGestures = true
            mapView.settings.rotateGestures = true
            mapView.settings.tiltGestures = true
            mapView.isIndoorEnabled = false

            if let mylocation = mapView.myLocation {
              print("User's location: \(mylocation)")
            } else {
              print("User's location is unknown")
            }
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()

            return mapView
        }

//        Updates the presented `UIView` (and coordinator) to the latestconfiguration.
        func updateUIView(_ mapView: GMSMapView, context: Self.Context) {
            // Creates a marker in the center of the map.
            marker.position = CLLocationCoordinate2D(latitude: 42.361145, longitude: -71.057083)
            marker.title = "Boston"
            marker.snippet = "USA"
            marker.map = mapView
        }
}

我以为我可以抓住 mapView.myLocation 并使用它在启动时手动聚焦地图相机,但这没有用。您可以在上面的代码中看到我尝试打印出位置,但我得到的只是“用户的位置未知”。此外,现在收到错误“无法将类型“GoogMapView”的值分配给类型“CLLocationManagerDelegate?”,我在其中输入“locationManager.delegate = self”。

有没有人知道在启动时让地图相机聚焦在用户当前位置的“正确”方式?

标签: swiftxcodeswiftuigoogle-maps-sdk-iosxcode11

解决方案


我以为我可以抓住 mapView.myLocation 并使用它在启动时手动聚焦地图相机,但这没有用。您可以在上面的代码中看到我尝试打印出位置,但我得到的只是“用户的位置未知”

看起来mylocation不包含任何位置坐标。mapView.myLocation可能不是您认为的那样,它可能保存用户的设备位置,而不是您传递给GMSCameraPosition的坐标。根据文档

如果启用了我的位置,则显示绘制用户位置点的位置。

所以它不是你传递给相机的那个

有没有人知道在启动时让地图相机聚焦在用户当前位置的“正确”方式?

您是否在某处创建了 locationManager 并处理了位置权限?如果你有,你可以在一个名为didUpdateLocations的函数中捕获用户的位置——在启动时检索用户位置需要几秒钟左右的时间。获得位置后,您可以将相机平移到该位置。


推荐阅读