lalala lalala - 3 months ago 29
Swift Question

Google Map SDK, iOS , cannot ge myLocation

I'd like to show my location on iOS app by using Google Maps SDK. However, it cannot get my location. I referred the following documents, document1, document2

This is my code. It only shows the map of United Kingdom.

Please help me to solve the problem.

import UIKit

class SearchVC: UIViewController,CLLocationManagerDelegate{

///Google Map
@IBOutlet weak var mapView:GMSMapView!
let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()

}

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


func locationManager(manager:CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){
if status == .AuthorizedWhenInUse{

locationManager.startUpdatingLocation()

mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager:CLLocationManager!, didUpdateLocations locations:[AnyObject]!){
if let location = locations.first as? CLLocation{

mapView.camera = GMSCameraPosition(target:location.coordinate, zoom:15,bearing:0, viewingAngle:0)
locationManager.stopUpdatingLocation()
}
}
}

Answer

Heres some code to parse your location... I think your just having an issue extracting the location info for the map view to load

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
    if (error != nil) {
        println("Error:" + error.localizedDescription)
        return
    }

    if placemarks.count > 0 {
        let pm = placemarks[0] as CLPlacemark
        pm.location.coordinate;
        mapView.camera = GMSCameraPosition(target:pm.location.coordinate, zoom:15,bearing:0, viewingAngle:0)
        locationManager.stopUpdatingLocation()

    }else {
        println("Error with data")
    }
}

I haven't compiled this code and I'm not swift savvy but hopefully this helps

To change your location go to Edit Scheme... enter image description here

Then select whatever location you want to simulate enter image description here