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

3

6
  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

Bungle answered 23/6, 2015 at 9:4 Comment(1)
If you are using Google Maps iOS SDK, seems there is no way to replace the default user location icon, a workaround is to put a marker on user's current position, you can see this answer for more details.Stirk
L
5

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.

Leitman answered 29/6, 2015 at 10:18 Comment(1)
Can anyone confirm if Google has issues with updating the blue dot and accuracy circle with a custom color? Since it can be done programatically and have the same effect...Muggins
A
5

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.

Adamandeve answered 30/6, 2015 at 21:44 Comment(3)
There is an example of this?Deherrera
No,then problem comes when user starts moving here and there,then there comes more than one marker.Hie
@Hie : You have to change position of marker.. you dont have to add marker on every movementPhineas
T
4

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.

Transvalue answered 20/11, 2017 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.