Customize Google Maps blue dot for current location

只愿长相守 提交于 2019-11-29 09:42:29

There is no way to do this with current version of the SDK (1.4.3), and actually there is an open issue with this request: Look here.

As a work around, you can hide the default button with:

 _map.myLocationEnabled = NO;

Then create a custom GMSMarker

 GMSMarker *pointMarker = [GMSMarker markerWithPosition:currentPosition];
 pointMarker.icon = [UIImage imageNamed:@"YourImage"];
 pointMarker.map = _map;

And change the position of it using a CLLocationManager, so it always show the current position. It's a bit tricky but is the only way, I could think, that you can achieve this. If you need a more complete example let me know.

bsod

Swift 4

class MasterMapViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {

    let currentLocationMarker = GMSMarker()

    override func viewDidLoad() {
        super.viewDidLoad()
        addCurrentLocationMarker()
    }

    func addCurrentLocationMarker() {

        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)
        currentLocationMarker.iconView = currentLocationMarkerView
        currentLocationMarker.isTappable = false
        currentLocationMarker.map = mapView

    }

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

        let lastLocation = locations.last!
        currentLocationMarker.position =  lastLocation.coordinate    
    }

}

Use this only as a starting point! This is not an attractive alternative because the constant updating of the marker's position on the map comes with a performance hit. If you were to go this route, find a way not to constantly update the marker's position from the location manager delegate.

For Swift 4.0

When you setup your GMSMapView:

mapView.isMyLocationEnabled = false

And when user location is updated:

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

    let userLocationMarker = GMSMarker(position: location.coordinate)
    userLocationMarker.icon = UIImage(named: "yourImageName")
    userLocationMarker.map = mapView   
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!