hello I am working with google maps and have the following questions
I want to display information of a marker in a fixed label and every time you touch will change the label.
I have the following code which I understand I can make the event a marker
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
self.nameService.text = "example"
return true
}
but this function does not work , ask the above function is for that?
I also tested this function to change the infowindow but neither works
func mapView(mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let index:Int! = Int(marker.accessibilityLabel!)
let customInfoWindow = NSBundle.mainBundle().loadNibNamed("CustomInfoWindow", owner: self, options: nil)[0] as! CustomInfoWindow
customInfoWindow.label.text = "example"
self.nameService.text = "example"
return customInfoWindow
}
I 'm making that mistake ?
thanks
try with the following code
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
self.nameService.text = "example"
return false
}
Related
for some odd reason when a marker is tapped google map will not show the snippet window in my swift code. I just dont see what im doing wrong. I get the marker on the map but when i tap it it does not show anything. Thank you in advance.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let position = place.coordinate
self.dismiss(animated: true) {
let marker = GMSMarker(position: position)
marker.title = "title here"
marker.snippet = "my snippet here"
marker.map = self.mapView
}
}
I had to remove the delegate below in order for the infowindow to show.
// func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// print("marker Tapped")
//
// return true
// }
You can put in the delegate
mapView.selectedMarker = marker
so the code will be as this :
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print("marker Tapped")
mapView.selectedMarker = marker
return true
}
Hope this will help you.
I want to add a custom info window to pop up in Google Maps whenever someone taps a pin. I've already hidden the current info window by not passing it any data and know that the function
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
return true
}
Handles what happens when a pin is tapped. I currently have this custom UIView and attributes on my MapViewController and the corresponding outlets added to the ViewController code:
How would I implement this UIView to pop up whenever I tap a pin?
you have to use GMSMapViewDelegate to use markerInfoWindow to your class.
let mapview = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapview.delegate = self
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
// Get a reference for the custom overlay
let index:Int! = Int(marker.accessibilityLabel!)
let customInfoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?[0] as! CustomInfoWindow
customInfoWindow.Timings.text = States[index].time
customInfoWindow.Title.text = States[index].name
customInfoWindow.Direction_Button.tag = index
customInfoWindow.Direction_Button.addTarget(self, action: #selector(GetDirectionsAction(sender:)), for: .touchUpInside)
return customInfoWindow
}
You should create a custom view for the annotation and then return that custom view for the following method:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
//Create and load your custom view here and then return the view
}
I'm using this simple MyMarker class
class MyMarker: GMSMarker {
var id: UInt32 = 0
}
so that my markers can also hold an additional numerical tag. When the user taps on my markers I call a segue to open a new scene the content's of which are dynamic and drawn with respect to the MyMarker's id. I want to do something like:
func mapView(mapView: GMSMapView, didTapMarker marker: MyMarker) -> Bool {
some_global_variable = marker.id;
performSegueWithIdentifier("segue", sender: nil)
return true
}
the problem of course is that the GMSMapViewDelegate expects marker to be of type GMSMarker.
How can I implement the behaviour I am after?
You need to type cast GMSMarker to your custom marker in it's delegate method, don't change signature of GMSMapViewDelegate methods.
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
if let myMarker = marker as? MyMarker {
some_global_variable = myMarker.id
performSegueWithIdentifier("segue", sender: nil)
}
return true
}
I know it is possible to capture the taps in the infowindow of a marker. I followed the documentation here.
All are written in Objective C so I tried converting it to Swift, here is my code:
func mapView(_ mapView: GMSMapView, didTap InfoWindowOfMarker: GMSMarker) {
print("You tapped infowindow")
}
But this isn't getting fired at all. What is wrong with the method?
You need to use the delegate of GMSMapView along with some prior setting see below.
Declare the use of GMSMapViewDelegate methods and set the delegate to self:
class yourClassName: UIViewController,GMSMapViewDelegate
mapView?.delegate = self
Method to detect tap on infoWindow:
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
print("infowindow tapped")
}
Method to detect tap on GMSMarker:
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
print("tapped on marker")
if marker.title == "myMarker"{
print("handle specific marker")
}
return true
}
Method to create custom infoWindow:
func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
let infoWindow = Bundle.main.loadNibNamed("nibName", owner: self, options: nil).first as! ClassName
infoWindow.name.text = "title"
infoWindow.address.text = "relevant address"
infoWindow.photo.image = UIImage(named: "imageName")
return infoWindow
}
I customized a infoWindow, but this shows a infowindow default in the background. Is there a way to hide the infoWindow background? or that I'm doing wrong.
The code is:
func mapView(mapView: GMSMapView!, markerInfoContents marker: GMSMarker!) -> UIView! {
if let infoView = UIView.viewFromNibName("MarkerInfoView") as? MarkerInfoView {
infoView.lblTitle.text = "Title" //marker.title
infoView.lblDescription.text = "Description"
infoView.lblDescription.numberOfLines = 0
infoView.lblDescription.lineBreakMode = NSLineBreakMode.ByWordWrapping
infoView.lblDescription.sizeToFit()
return infoView
} else {
return nil
}
}
in your nib file, where you do custom look for your info window, set top nib view background color to clear color
Set your custom view as the marker image instead of the info window.
Convert your custom view into image and set it as marker image.
That will solve your purpose.
I think you need to implement this method and return your custom view.
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView?
make sure that you don't have
marker.title = "something"
anywhere in your code. Then the default will always show unless you use
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
<#code#>
}
to set your infoWindow properties.
If you are using a customInfoWindow, then you don't have to access GMSMarker() properties like marker.snippet and marker.title
Even if you use them by mistake in your code, then you need to return an empty view in delegate method:
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}