0

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()
    }
}
}
Community
  • 1
  • 1
Toshi
  • 1,293
  • 3
  • 19
  • 37

2 Answers2

0

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

jsetting32
  • 1,632
  • 2
  • 20
  • 45
  • made a typo in `GMSCameraPosition()`. Make sure to include `target:` before `pm.location.coordinate` – jsetting32 Dec 20 '14 at 10:32
  • I have modified the code as you told, and the result has changed. However, it show Apple address as my location, when I pressed my location button on iOS simulator. Could you tell me how to solve this situation? – Toshi Dec 20 '14 at 10:38
  • There are only a handful of locations you can simulate through your iOS Simulator... So the only way you can actually get YOUR EXACT location is running the app on your phone and getting the location that way... – jsetting32 Dec 20 '14 at 10:40
0

The problem:

func locationManager(manager:CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)

This function is only truly called when the authorization status changes. For instance when you first run the app, it will ask to enable location services. Once you hit yes, this function will run. Next time you run the app, the authorization status won't change, because it was previously enabled.

The fix:

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.startUpdatingLocation()

        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
    }
}

Now everytime the view loads, it will start updating location as opposed to when the authorization status changes. Make sure that you also add the key: value pair into info.plist (NSLocationWhenInUseUsageDescription: {authorizatinon message string}).

Cody Moorhouse
  • 227
  • 2
  • 5