User custom location on Google maps for iOS (GMSMapview)

  • Is there an official way to set a custom user location point in Google maps for iOS (GMSMapView)?
  • Is there a known way to "hack" it? How do I iterate through all the dungeons and layers and the blue dot fish?
  • Even if you cannot customize your appearance, can you control the z-index? When you have many markers, the little blue dot becomes hidden and sometimes you want it to be always visible.

thank

+3


source to share


3 answers


You can try to find the image:

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



I did a quick search on these and the only related file I found with this blue dot is GMSSprites-0-1x.

Please check the terms and conditions of Google Maps as this may not be legal.

+5


source


You can install maps myLocationEnabled

on NO

. This will hide the default location point. Then use an instance CLLocationManager

to provide you with your position. Inside the method, CLLocationManager

didUpdateLocations

you can set a custom GMSMarker

. Set its icon property to whatever you want your point to look like using [UIImage imageNamed:]

. This will allow you to achieve the desired effect.



+5


source


Swift 4

Disable Google Map default current location marker (disabled by default):

mapView.isMyLocationEnabled = false

      

Create a token as a property of the view controller instance (because the delegate will need access to it):

let currentLocationMarker = GMSMarker()

      

The initializer GMSMarker

allows you to use UIImage

either UIView

as custom graphics, and not UIImageView

, unfortunately. If you want more control over the graphics use UIView

. In loadView

or viewDidLoad

(wherever you have configured the map), customize 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's left is giving the marker a coordinate (initially and every time the user's location changes) that you execute through the delegate CLLocationManagerDelegate

.

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 the location manager creates may not be very accurate (although sometimes they are), so try adding your custom marker first. You can wait until the location manager has collected a few coordinates before applying it to your custom marker, waiting until locations.count > someNumber

, but I don't find this approach very attractive.

0


source







All Articles