swift - 如何使用 SwiftUI 和 GoogleMaps iOS SDK 关注用户位置

标签 swift xcode swiftui google-maps-sdk-ios xcode11

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

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

调用我的 Google map View 的主视图在这里:


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 map View (我称之为 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 并使用它在启动时手动聚焦 map 相机,但那没有用。您可以在上面的代码中看到我尝试打印出位置,但我得到的只是“用户的位置未知”。 此外,现在出现错误“无法将类型‘GoogMapView’的值分配给类型‘CLLocationManagerDelegate?’”,我在其中输入“locationManager.delegate = self”。

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

最佳答案

I thought i could grab mapView.myLocation and use that to manually focus the map camera on startup but that didnt work. You can see in the code above i try to print out the location but all i get back is 'User's location is unknown'

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

If My Location is enabled, reveals where the user location dot is being drawn.

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

Does anyone have an idea of the 'proper' way to have the map camera focus on a user's current location at startup?

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

关于swift - 如何使用 SwiftUI 和 GoogleMaps iOS SDK 关注用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58309458/

相关文章:

json - 如何将字典转换为不带空格和换行符的json字符串

swift - 使用 SQLite.swift 获取 swift 循环中的最后一行

uitableview - SwiftUI:内容与中心对齐的自定义表格 View 单元格

ios - 细节 View 顶部的额外空间不会消失(SwiftUI)

swift - @Published 数组中 ObservableObject 的更改不会更新 View

swift - 将 UIView 置于状态栏上方

c++ - boost::arm64 的上下文?

ios - Swift:类AppDelegate中的线程1信号SIGABRT:UIResponder,UIApplicationDelegate

xcode - 使用 Xcode 开发网页

swift 3(SpriteKit): Making a projectile move