Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom marker InfoWindow in google maps iOS swift?

I'm using google map iOS in my app , i want to show custom marker info window.How can i create it? I'm using storyboards and swift as the language.

enter image description here

like image 223
Anushka Madushan Avatar asked Feb 23 '17 11:02

Anushka Madushan


People also ask

How to add custom marker in google map in swift?

Adding a marker 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. The marker is created at coordinates 10,10 , and displays the string "Hello world" in an info window when clicked.

How do I put InfoWindow on marker?

Move an info windowAttach the info window to a new marker using the InfoWindow. open() method. Note: If you call open() without passing a marker, the InfoWindow will use the position specified upon construction through the InfoWindowOptions object literal.

How do you delete a marker on Google Maps?

You can delete the markers by removing them from the map and then setting the array's length to 0 , which removes all references to the markers.


1 Answers

I had one such problem with my app.

  1. In your ViewController.swift:

     class ViewController: GMSMapViewDelegate {
            func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
    
                    let placeMarker = marker as! PlaceMarker
    
                    if let infoView = UIView.viewFromNibName("MarkerInfoView") as? MarkerInfoView {
                        infoView.nameLabel.text = placeMarker.place.name
    
                        if let photo = placeMarker.place.photo {
                            infoView.placePhoto.image = photo
                        } else {
                            infoView.placePhoto.image = UIImage(named: "generic")
                        }
    
                        return infoView
                    } else {
                        return nil
                    }
                }
            }
    

2.Then create a UIView class:

import UIKit

class MarkerInfoView: UIView {

    @IBOutlet weak var placePhoto: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
}
  1. Then create a xib file named the same as UIView class:

Like this

Note that the class name is the same as UIView class. You can add label and images as per your requirement. Connect those IBOutlets to the UIView class as shown.

Hope this helps you. All the Best!

like image 191
Ara Rose Avatar answered Oct 02 '22 22:10

Ara Rose