Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the UIView instance for a Google Map marker?

I want to show a VC as a pop up when the user taps on one of the markers on the Google Map.

The reason that I want to do this is because I want to control the view that pops up when the marker is tapped. I tried using the mapView(mapView: GMSMapView, markerInfoWindow marker: GMSMarker) delegate method. But I don't know how to create a view controller in that method that controls the marker info window's view.

To present a VC as a pop over, I did this:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MarkerInfoController")
vc.modalInPopover = true
vc.modalPresentationStyle = .Popover
print(marker.iconView)
vc.popoverPresentationController!.sourceView = marker.iconView

self.presentVC(vc) // this is from EZSwiftExtensions. Don't worry about it

The problem arises when I try to set the sourceView of the UIPopoverPresentationController. I thought using the iconView property would work, but no. There is always an error saying that sourceView is not set.

How can I get the UIView instance for the marker, so that I can assign it to sourceView?

P.S. This is how a marker is created:

func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {
    let marker = GMSMarker(position: coordinate)
    marker.map = mapView
}
like image 223
Sweeper Avatar asked Sep 09 '16 16:09

Sweeper


People also ask

How do I add a marker to a gmsmapview object?

To add a marker, create a GMSMarker object that includes a position and title, and set its map. The following example demonstrates how to add a marker to an existing GMSMapView object.

How are markers displayed on the map?

All markers are displayed on the map in order of their zIndex, with higher values displaying in front of markers with lower values. By default, markers are displayed according to their vertical position on screen, with lower markers appearing in front of markers further up the screen.

Where to place scanned maps in ui-view32?

The closer the two points are to two opposite corners the better. The advantage of placing the scanned map in the MAPS folder is it will always be available in the list of maps in UI-View. Plug-in map servers integrate seamlessly into UI-View32, and provide interactive maps that can be zoomed and panned.

How do I import a saved map into UI-view?

Once you have the lat / long converted to the right format, go to the folder that you saved the map in and rename it if desired. Then move or copy it to the MAPS folder below where you have UI-View installed. Finally, drag it from there to the normal map view in UI-View.


1 Answers

Output: enter image description here

Code:

import UIKit
import GoogleMaps

class MapViewController: UIViewController {
  
  @IBOutlet weak var mapView: GMSMapView!
  var sourceView: UIView?
}

extension MapViewController: GMSMapViewDelegate {
  
  func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    mapCenterPinImage.fadeOut(0.25)
    
    // Adding a delay becuase when click on marker Camera changes it position
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
      
      let location = marker.accessibilityActivationPoint
      self.sourceView = UIView(frame: CGRect(x: location.x, y: location.y, width: 1, height: 1))
      self.view.addSubview(self.sourceView!)
    
      let popController = MyPopUpViewController()
      popController.modalPresentationStyle = UIModalPresentationStyle.popover
      popController.preferredContentSize = CGSize(width: 200, height: 200)
      popController.popoverPresentationController?.delegate = self
      popController.popoverPresentationController?.sourceView = self.sourceView
      self.present(popController, animated: true)
    }

    return false
  }
  
}
extension MapViewController: UIPopoverPresentationControllerDelegate{
  func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
  }
  func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
    sourceView?.removeFromSuperview()
    sourceView = nil
    return true
  }
  
}

What I've done basically created a UIView and added it to ViewController at runtime and set this as a source of Popup and make it nil when it's dismissed. marker.accessibilityActivationPoint is the source of X and Y according to device's screen

like image 200
Sahil Avatar answered Sep 28 '22 18:09

Sahil