【问题标题】:viewDidLoad resetting Google Maps view after updating the current locationviewDidLoad 更新当前位置后重置谷歌地图视图
【发布时间】:2019-03-11 21:23:28
【问题描述】:

我对 swift/xcode 还很陌生,所以这可能只是 viewDidLoad 的一个简单问题,我正在寻找,但这是正在发生的事情。我正在开发一个应用程序,它可以获取您的当前位置并使用 Google Maps/Places API 显示您周围最近的滑雪山。如果我直接从导航控制器运行场景,地图会正确显示所有山脉的标记。但是我正在实现一个登录功能,因此如果我在登录后加载场景,地图只会重置为 viewDidLoad 函数中的原始坐标。我的代码发布在下面。我的问题是首先,我该如何修复它以使其不会重置为原始坐标,其次,为什么只有当我从不是导航控制器的场景中运行它时才会这样做?

import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation

class MountainFinderController: UIViewController, CLLocationManagerDelegate {

    let mountainModel = skiMountainData()


    var placesClient: GMSPlacesClient?
    var locationManager = CLLocationManager()
    lazy var mapView = GMSMapView()
    var currentLocation: CLLocationCoordinate2D!
    var mountainMarkers = [GMSMarker]()

    typealias JSONDictionary = [String: Any]

    override func viewDidLoad() {
        super.viewDidLoad()

        //creates initial map view
        let cameraGMAPS = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 8)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: cameraGMAPS)
        mapView.isMyLocationEnabled = true
        self.view = mapView
        print("created init map")

        //requests authorization to use location
        locationManager.requestAlwaysAuthorization()
        self.locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        print("done loading view")
    }


    override func loadView() {

    }

    //called when startUpdatingLocation is called
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        //gets user location
        let userLocation = locations.last
        currentLocation = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)
        //moves the camera to the users current location and shows their current location on the map
        let cameraGMAPS = GMSCameraPosition.camera(withLatitude: userLocation!.coordinate.latitude,                                                  longitude: userLocation!.coordinate.longitude, zoom: 8)
        mapView = GMSMapView.map(withFrame: self.view.bounds, camera: cameraGMAPS)
        mapView.isMyLocationEnabled = true
        self.view = mapView

        //function call to get nearest 20 mountains
        mountainModel.getNearbyMountains(lat: locationManager.location!.coordinate.latitude, long: locationManager.location!.coordinate.longitude)
        //adds a marker for each mountain
        print("adding markers")
        addMarkers()
        print("added markers")

        //stops updating the location
        locationManager.stopUpdatingLocation()
    }

    //func to add a marker for each mountain to the map
    func addMarkers(){
        mountainMarkers.removeAll()
        for currMountain in mountainModel.getSkiMountainData() {
            let mountainMarker = GMSMarker()
            mountainMarker.position = CLLocationCoordinate2D(latitude: currMountain.getLat(), longitude: currMountain.getLong())
            mountainMarker.title = currMountain.getName()
            mountainMarker.map = mapView
            mountainMarkers.append(mountainMarker)
        }
        mountainModel.setMountainMarkers(markers: mountainMarkers)
    }
}

【问题讨论】:

  • 我不确定您所看到的确切内容,但您请求位置的方式并不理想。首先,您应该只根据您的需要请求always whenInUse。此外,requestLocation 是获得“一次性”位置的更好方法。它将尝试提供准确的位置。 startUpdatingLocation 后跟 stopUpdatingLocation 可能会导致交付单个不准确的位置。
  • 一旦您验证了位置权限,或者收到了didChangeAuthorization的回调,您应该requestLocation
  • 我请求位置的方式虽然可能并不理想,但它现在正在工作,我知道这不是问题。发生的事情是,当我从根故事板开始使用 MountainFinder 场景运行应用程序时,它会正确显示带有我的位置和所有标记的地图。当我在登录场景之后使用 MountainFinder 场景运行应用程序时,我得到的只是以纬度 0 和经度 0 为中心的地图,虽然我当前的位置仍在地图上,但没有标记,相机只是没有以我的位置为中心
  • 我建议您设置一些断点以确认您的didUpdateLocations 在第二种情况下被调用。我还强烈建议您采用我描述的requestLocation 方法,因为您的问题听起来像是时间问题。
  • 您正在使用 mapView = GMSMapView.map(withFrame: self.view.bounds, camera: cameraGMAPS) 行创建一个新的 GPSMapView didUpdateLocations。相反,您需要更新现有的。

标签: ios swift google-maps google-api


【解决方案1】:

想通了。 Drewster 是对的,我必须更新

mapView with mapView.camera = cameraGMAPS

谢谢大家

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    相关资源
    最近更新 更多