简体   繁体   中英

iOS Google Maps SDK Not showing user location

I'm working on an app that uses google maps to display a set of pins, and I want the map's camera to start on the user's location. I tried writing the following code (in Swift 2.0) but it doesn't put me in the device's current location, instead it shows me a blank blue screen (which I have found to be the exact middle of the map). Here's a screenshot of what I have

这是我所拥有的屏幕截图

I used the following libraries while writing this code:

Google Maps SDK

OneShotLocationManager

Here's the code that I have (in ViewController.swift)

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {

    var manager: OneShotLocationManager?

    var userlat: Double = 0.0
    var userlng: Double = 0.0

    override func viewDidLoad() {
        manager = OneShotLocationManager()
        manager!.fetchWithCompletion {location, error in

            // fetch location or an error
            if let loc = location {
                print(loc)
                self.userlat = loc.coordinate.latitude
            } else if let err = error {
                print(err.localizedDescription)
            }
            self.manager = nil
        }


        super.viewDidLoad()

        let camera = GMSCameraPosition.cameraWithLatitude(userlat,
            longitude: userlng, zoom: 6)

        let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        mapView.delegate = self
        self.view = mapView

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
    }

    func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
        let infoWindow = NSBundle.mainBundle().loadNibNamed("CustomInfoWindow", owner: self, options: nil).first! as! CustomInfoWindow
        infoWindow.label.text = "\(marker.position.latitude) \(marker.position.longitude)"
        return infoWindow
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

And here's the console output after running the app. Note that it does output the user's current location:

<+51.50998000,-0.13370000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 2/3/16, 8:47:57 AM Pacific Standard Time 2016-02-03 08:47:57.545 Hanky Ranky[3379:260848] Google Maps SDK for iOS version: 1.11.21919.0

Thanks for the help guys!

Maybe you are missing two properties in ViewController.swift.

var locationManager = CLLocationManager()

var didFindMyLocation = false

The locationManager property will be used to ask for the user's permission to keep track of his/her location, and then based on the authorization status to either display his/her current location or not. The didFindMyLocation flag will be used so we know whether the user's current position was spotted on the map or not, and eventually to avoid unnecessary location updates.

This Swift Tutorial for Google Maps might be helpful.

class ViewController: UIViewController, GMSAutocompleteViewControllerDelegate
{


    var placeName: String = "Default"
    var userlat: CLLocationDegrees = 0
    var userlng: CLLocationDegrees = 0

    override func viewDidLoad() {
    super.viewDidLoad()


    let acController = GMSAutocompleteViewController()
    acController.delegate = self
    self.presentViewController(acController, animated: true, completion: nil)




    // Do any additional setup after loading the view.
    }

    override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let camera = GMSCameraPosition.cameraWithLatitude(userlat,
        longitude: userlng, zoom: 6)

    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    mapView.delegate = self


    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

    self.view = mapView

   }


  func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithPlace place: GMSPlace!) {

    lat = place.coordinate.latitude
    longi = place.coordinate.longitude
    placeName = place.name
   }

}

I hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM