2

I have a method which places a marker to the map which I have a custom callout for. The problem is the user needs to click this marker in order to bring it up. Is it possible to call a method from Google Map's API which can select the annotation and bring up the callout programmatically?

I know that for Apple maps there is [mapView selectAnnotation:annotationName animated:YES]; in Objective C and self.mapView.selectAnnotation(selectedAnnotation, animated: true) for Swift

I have read through How to select a map pin programmatically but this doesn't seem to be any help for Google maps.

FYI - This is a Swift project

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Niall Kiddle
  • 1,477
  • 1
  • 16
  • 35
  • I think you can assign a marker in `self.mapView?.selectedMarker`property something like `self.mapView?.selectedMarker = yourDesiredMarker` I will test it – Reinier Melian Mar 07 '18 at 16:36
  • Yeah it required me making an `array: [GMSMarker]`. Adding the marker to the array when it was initialised. and then using `selectMarker` at the index 0. Need to make sure I clear the array and map every time before I create a marker. @ReinierMelian – Niall Kiddle Mar 07 '18 at 19:53

1 Answers1

2

You need to use GMSMapView selectedMarker property, also you need to move the camera to the new selectedMarker position

Full Code example

import UIKit
import GoogleMaps

struct MarkerStruct {
    let name: String
    let lat: CLLocationDegrees
    let long: CLLocationDegrees
}

class TapViewController: UIViewController, GMSMapViewDelegate {
    let markers = [
        MarkerStruct(name: "Food Hut 1", lat: 52.649030, long: 1.174155),
        MarkerStruct(name: "Foot Hut 2", lat: 35.654154, long: 1.174185),
        MarkerStruct(name: "Foot Hut 3", lat: 22.654154, long: 1.174185),
        MarkerStruct(name: "Foot Hut 4", lat: 50.654154, long: 1.174185),
        ]

    var mapView : GMSMapView? = nil

    var mapMarkers : [GMSMarker] = []
    var timer : Timer? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        let camera = GMSCameraPosition.camera(withLatitude: 52.649030, longitude: 1.174155, zoom: 14)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

        mapView?.delegate = self
        self.view = mapView

        for marker in markers {
            let position = CLLocationCoordinate2D(latitude: marker.lat, longitude: marker.long)
            let locationmarker = GMSMarker(position: position)
            locationmarker.title = marker.name
            locationmarker.map = mapView
            mapMarkers.append(locationmarker)
        }

        timer = Timer.scheduledTimer(timeInterval: 15, target: self, selector:  #selector(self.selectRandomMarker), userInfo: nil, repeats: true)

    }

    //Selecting random marker
    func selectRandomMarker(){
        let randomIndex = arc4random_uniform(UInt32(self.mapMarkers.count))
        self.mapView?.selectedMarker = self.mapMarkers[Int(randomIndex)]
        self.mapView?.animate(toLocation: (self.mapView?.selectedMarker!.position)!)
    }

}
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55