user3185748
user3185748

Reputation: 2538

How to Hide Google Maps Location Dot

I'm drawing a bit of a blank, I want to hide the blue location dot from the Google Maps iOS API. But if I set

self.mapView.myLocationEnabled = false

Then all location services are halted. I only need to temporarily disable the blue dot, is there a way to make it clear while continuing to update locations in the background?

Upvotes: 2

Views: 4789

Answers (4)

Aishwary Dhare
Aishwary Dhare

Reputation: 539

do this right after you have initialised your mapView

mapView.settings.myLocationButton = false

Upvotes: 2

Mina Eskander
Mina Eskander

Reputation: 1

Configure Camera Position

let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 48.857165, longitude: 2.354613, zoom: 8.0)        

viewMap.camera = camera
viewMap.delegate = self

Initialize circle with radius 0

let circle = GMSCircle.init(position: camera.target, radius: 0)
circle.fillColor = UIColor.clear
circle.map = viewMap

Upvotes: -1

gurmandeep
gurmandeep

Reputation: 1247

I tested the same, i added below line of code into my application and displayed the user location in Google maps.

mapView.myLocationEnabled=YES;

If i totally remove the above line of code, it displays all other annotations and does not show the user location

Upvotes: 2

Cody Moorhouse
Cody Moorhouse

Reputation: 257

Took me awhile but I think I got it working.

In your viewDidLoad():

    // MARK: Location Settings
    // Location Manager
    locationManager.requestWhenInUseAuthorization()

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

        mapView.delegate = self
        mapView.settings.myLocationButton = true

    }

You need this bit of code to assign the delegates and to start the updating of location. This will also add the location button to the google map. You will also have to make sure you add the authorization message into the info.plist.

{key=NSLocationWhenInUseUsageDescription}: {type=String}: {value=custom message}.

And then you will need to extend these delegates (place them at the bottom of your MapViewController outside your class):

// MARK: - Extension: LocationManager
extension MapViewController: CLLocationManagerDelegate {
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedWhenInUse {
            locationManager.startUpdatingLocation()
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {
            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
            locationManager.stopUpdatingLocation()
        }
    }
}

// MARK: - Extension: GMSMapView
extension MapViewController: GMSMapViewDelegate {
    func didTapMyLocationButtonForMapView(mapView: GMSMapView) -> Bool {
        locationManager.updateLocation()
        return false
    }
}

CLocationManagerDelegate:

  1. didChangeAuthorizationStatus only gets called when locationServices are enabled/disabled.
  2. didUpdateLocations is called when startUpdating is active. So it will start when the view loads (as in viewDidLoad) and then will stop updating the location once it has the location.

GMSMapViewDelegate:

  1. didTapMyLocationButton is called whenever the location button is clicked.

Upvotes: 0

Related Questions