Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

ios - Current Location in Google Maps with swift

I'm trying to display the user's current location on a google map but in the case below, the map doesn't even get displayed. What should I change to fix this?

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()
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this bellow code its working fine

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()
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...