1

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()
}
Gyro Technologies
  • 47
  • 1
  • 2
  • 11

6 Answers6

9

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()
}
Raksha Saini
  • 604
  • 12
  • 28
1

do requier setting on infoPlist and then try this

@IBOutlet weak var your "name of view which show map": GMSMapView!

override func viewDidLoad(){
        super.viewDidLoad()

  placesClient = GMSPlacesClient.shared()
  locationManager.requestAlwaysAuthorization()
     if CLLocationManager.locationServicesEnabled(){     
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        locationManager.distanceFilter = 500
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()  
  }   
    mapView.settings.myLocationButton = true
    mapView.settings.zoomGestures = true
    mapView.animate(toViewingAngle: 45)
    mapView.delegate = self  }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     let newLocation = locations.last // find your device location
     mapView.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 14.0) // show your device location on map
     mapView.settings.myLocationButton = true // show current location button
     var lat = (newLocation?.coordinate.latitude)! // get current location latitude
     var long = (newLocation?.coordinate.longitude)! //get current location longitude

}
Masoud Roosta
  • 455
  • 9
  • 19
0

The problem is that you are setting the mapView's frame to CGRectZero. This causes the map to have zero height and zero width, no wonder it does not show!

Try setting it to CGRectMake(0,0,200,200) for example, this will give you a map at the left top of the screen with a size of 200 x 200.

I have never used Swift before, so the syntax might be a little different for CGRectMake()

Balázs Vincze
  • 3,550
  • 5
  • 29
  • 60
0

It seems that your creation of the map isn't in your viewDidLoad function. You may want to try moving that there and see what happens.

bwc
  • 1,732
  • 1
  • 14
  • 27
0

Add the appropriate properties into the info.plist.

You should put make sure you have the NS properties of locationalwaysusagedescription and wheninuseusagedescription in the information properties list. This allows for the permissions of the current location to be asked.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Assel Kash
  • 111
  • 3
  • 17
0
import UIKit

import GoogleMaps import GooglePlaces import CoreLocation

class MapsViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {

var mapView = GMSMapView()
var locationManager = CLLocationManager()
let marker = GMSMarker()

override func viewDidLoad(){
    super.viewDidLoad()
    
    mapView.frame = self.view.bounds
    self.view.addSubview(mapView)
    
    locationManager.requestAlwaysAuthorization()
    if CLLocationManager.locationServicesEnabled(){
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 10
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
    mapView.settings.myLocationButton = true
    mapView.settings.zoomGestures = true
    mapView.animate(toViewingAngle: 45)
    mapView.delegate = self
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let newLocation = locations.last // find your device location
    mapView.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 14.0) // show your device location on map
    mapView.settings.myLocationButton = true // show current location button
    let lat = (newLocation?.coordinate.latitude)! // get current location latitude
    let long = (newLocation?.coordinate.longitude)! //get current location longitude
    
    marker.position = CLLocationCoordinate2DMake(lat,long)
    marker.map = mapView
    print("Current Lat Long - " ,lat, long )
}

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    mapView.clear()
    DispatchQueue.main.async {
        let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
        self.marker.position = position
        self.marker.map = mapView
        self.marker.icon = UIImage(named: "default_marker")
        print("New Marker Lat Long - ",coordinate.latitude, coordinate.longitude)
    }
}

}

Shubham
  • 11
  • 3