我正在尝试在google地图上显示用户的当前位置,但在以下情况下,该地图甚至都不会显示。我应该改变些什么来解决这个问题?

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    //user location stuff
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error" + error.description)
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations.last
    let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

    let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
        longitude: userLocation!.coordinate.longitude, zoom: 8)
    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    self.view = mapView

    let marker = GMSMarker()
    marker.position = center
    marker.title = "Current Location"
    marker.snippet = "XXX"
    marker.map = mapView

    locationManager.stopUpdatingLocation()
}

最佳答案

您可以尝试下面的代码,它的工作正常

import UIKit
import GoogleMaps
import GooglePlaces

class SearchMapsViewController: UIViewController,
     UINavigationBarDelegate, GMSAutocompleteFetcherDelegate,
     LocateOnTheMap, UISearchBarDelegate, CLLocationManagerDelegate
{

   @IBOutlet var googleMapsContainerView: UIView!
   var searchResultController: SearchResultsController!
   var resultsArray = [String]()
   var googleMapsView:GMSMapView!
   var gmsFetcher: GMSAutocompleteFetcher!
   var locationManager = CLLocationManager()

override func viewDidAppear(animated: Bool) {
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()

    self.googleMapsView = GMSMapView (frame: self.googleMapsContainerView.frame)
    self.googleMapsView.settings.compassButton = true
    self.googleMapsView.myLocationEnabled = true
    self.googleMapsView.settings.myLocationButton = true
    self.view.addSubview(self.googleMapsView)
    searchResultController = SearchResultsController()
    searchResultController.delegate = self
    gmsFetcher = GMSAutocompleteFetcher()
    gmsFetcher.delegate = self
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
    print("Error" + error.description)
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let userLocation = locations.last
    let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

    let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude, zoom: 15);
    self.googleMapsView.camera = camera
    self.googleMapsView.myLocationEnabled = true

    let marker = GMSMarker(position: center)

    print("Latitude :- \(userLocation!.coordinate.latitude)")
    print("Longitude :-\(userLocation!.coordinate.longitude)")
    marker.map = self.googleMapsView

    marker.title = "Current Location"
    locationManager.stopUpdatingLocation()
}

10-07 21:19