Custom user location dot in Google maps for iOS (GMSMapview)

8.1k views Asked by At
  1. Is there an official way to set a custom user location dot in Google maps for iOS (GMSMapView)?
  2. Is there a known way to "hack" it? Like iterating through all subviews and layers and fish the blue dot?
  3. Even if you can't customise its appearance, can you control its z order index? When you have many markers, the little blue dot becomes hidden, and sometimes you want it to be visible at all times.

Thanks

3

There are 3 answers

1
apinho On

You can try to find the image on:

GoogleMaps.framework > Resources > GoogleMaps.bundle OR GoogleMaps.framework > Resources > GoogleMaps.bundle > GMSCoreResources.bundle

I did a quick search on those and the only associated file I found with that blue dot is GMSSprites-0-1x.

Please read the google maps terms and conditions because this might not be legal.

3
i2Fluffy On

You can set the maps myLocationEnabled to NO. That will hide the default location dot. Then use an instance of CLLocationManager to give you your position. Inside CLLocationManager didUpdateLocations method you can set a custom GMSMarker. Set its icon property to whatever you want your dot to look like using [UIImage imageNamed:]. This will allow you to achieve the desired effect.

0
lurning too koad On

Swift 4

Disable the default Google Map current location marker (it's disabled by default):

mapView.isMyLocationEnabled = false

Create a marker as an instance property of the view controller (because a delegate will need access to this):

let currentLocationMarker = GMSMarker()

The GMSMarker initializer allows for a UIImage or a UIView as a custom graphic, not a UIImageView unfortunately. If you want more control over the graphic, use a UIView. In your loadView or viewDidLoad (wherever you configured the map), configure the marker and add it to the map:

// configure custom view
let currentLocationMarkerView = UIView()
currentLocationMarkerView.frame.size = CGSize(width: 40, height: 40)
currentLocationMarkerView.layer.cornerRadius = 40 / 4
currentLocationMarkerView.clipsToBounds = true
let currentLocationMarkerImageView = UIImageView(frame: currentLocationMarkerView.bounds)
currentLocationMarkerImageView.contentMode = .scaleAspectFill
currentLocationMarkerImageView.image = UIImage(named: "masterAvatar")
currentLocationMarkerView.addSubview(currentLocationMarkerImageView)

// add custom view to marker
currentLocationMarker.iconView = currentLocationMarkerView

// add marker to map
currentLocationMarker.map = mapView

All that remains is giving the marker a coordinate (initially and every time the user's location changes), which you do through the CLLocationManagerDelegate delegate.

extension MapViewController: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let lastLocation = locations.last!

        // update current location marker 
        currentLocationMarker.position = CLLocationCoordinate2D(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)   

    }

}

The first few locations that the location manager produces may not be very accurate (although sometimes it is), so expect your custom marker to jump around a bit at first. You can wait until the location manager has gathered a few coordinates before applying it to your custom marker by waiting until locations.count > someNumber but I don't find this approach very attractive.