Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify which GMSMarker was tapped - iOS, Swift

I'm quite new to Swift and i'm trying to better understand Google Maps API. I'm building a simple app that shows images when markers on panoramaView are tapped through didTapMarker method. Since each marker should show a different images, i'm trying to find a way to identify which marker has been tapped, a sort of marker tag. All suggestions are welcome.

Down here is a prototype of the code with 2 markers and 2 images. Not really sure how to do it, but didTapMarker method should show randomImage when marker is tapped and randomImage2 when marker2 is tapped. So far it only shows randomImage when both marker and marker1 are tapped.

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSPanoramaViewDelegate {

@IBOutlet weak var viewStreet: UIView!
@IBOutlet weak var randomImage: UIImageView!
@IBOutlet weak var randomImage2: UIImageView!

var panoView: GMSPanoramaView!

override func viewDidLoad() {
    super.viewDidLoad()

    randomImage.hidden = true
    randomImage2.hidden = true

    let panoView = GMSPanoramaView(frame: CGRectMake(200, 200, 400, 400))
    panoView.delegate = self
    panoView.moveNearCoordinate(CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312))

    viewStreet.addSubview(panoView)
    viewStreet.sendSubviewToBack(panoView)

    let position = CLLocationCoordinate2D(latitude: -33.732, longitude: 150.312)
    let marker = GMSMarker(position: position)
    marker.panoramaView = panoView

    let position2 = CLLocationCoordinate2D(latitude: -33.732, longitude: 150.311)
    let marker2 = GMSMarker(position: position2)
    marker2.panoramaView = panoView
}

func panoramaView(panoramaView: GMSPanoramaView, didTapMarker marker: GMSMarker) -> Bool {

    randomImage.hidden = false
    randomImage2.hidden = true
    return true
}
}

EDIT: solved, thanks to everyone, i'm adding a trivial example on how to do it then.

 marker.userData = "example" 

Then didTapMarker method is always called when a marker is tapped, but randomImage 's propriety is set to false only when the marker tapped is the one above.

 func panoramaView(panoramaView: GMSPanoramaView, didTapMarker marker: GMSMarker) -> Bool {

    if marker.userData as? String == "example" {
         randomImage.hidden = false
    }
    return true
}

Code can be improved making use of a dictionary to handle multiple markers but it's up to you. :)

like image 572
Don G Avatar asked Jun 15 '16 21:06

Don G


1 Answers

put the data of that marker in userData of that marker. Make use of that that whenever marker is tapped in didTapInfoWindowOfMarker api.

like image 192
Algo_Ranjan Avatar answered Nov 15 '22 22:11

Algo_Ranjan