viewDidLoad更新当前位置后重置Google Maps视图

时间:2019-03-11 21:23:28

标签: ios swift google-maps google-api

我对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)
    }
}

1 个答案:

答案 0 :(得分:0)

弄清楚了。 Drewster是正确的,我必须更新

mapView with mapView.camera = cameraGMAPS

谢谢你们