10

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. :)

Don G
  • 125
  • 1
  • 8

1 Answers1

17

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

Algo_Ranjan
  • 186
  • 1
  • 7
  • 1
    So `marker.userData = randomImage`, ok. Then what should i do? I cannot use method `didTapInfoWindowOfMarker` cause i'm placing markers in PanoramaView which does not support it as far as i know. Ty and pardon, i'm still learning. – Don G Jun 16 '16 at 21:30
  • Looks like you could use `panoramaView:didTapMarker:` on `GMSPanoramaViewDelegate`. – Saxon Druce Jun 21 '16 at 01:38
  • @SaxonDruce ty for info, solved then. I've edited the first post with a sample. :) – Don G Jun 21 '16 at 07:47
  • Marker userdata null returns when pushing another vc and returning to previous vc. How to solve it.Help me. – Karthi Rasu Jan 08 '22 at 16:18