Swift 3 Google Map Add Markers on Touch

Swift 3 Google Maps iOS SDK - Add Marker via Touch

First you need to add a UILongPressGestureRecognizer to your GMSMapView and implementing the UIGestureDelegate protocol to make it work simultaneously with all gestures of GMSMapView after that in longPress action you should translate the CGPoint of touch and convert it in CLLocationCoordinate2Dthe remain is trivial

Use this as example

//
// DashedOverlayViewController.swift
// GoogleMapsExample
//
// Created by Reinier Melian on 17/07/2017.
// Copyright © 2017 Pruebas. All rights reserved.
//

import UIKit
import GoogleMaps
import GooglePlaces

class DashedOverlayViewController: UIViewController {

@IBOutlet weak var mapView: GMSMapView!

var arrayCoordinates : [CLLocationCoordinate2D] = []

var longPressRecognizer = UILongPressGestureRecognizer()

@IBAction func longPress(_ sender: UILongPressGestureRecognizer) {
debugPrint("You tapped at YES")
let newMarker = GMSMarker(position: mapView.projection.coordinate(for: sender.location(in: mapView)))
self.arrayCoordinates.append(newMarker.position)
newMarker.map = mapView
}

override func viewDidLoad() {
super.viewDidLoad()

longPressRecognizer = UILongPressGestureRecognizer(target: self,
action: #selector(self.longPress))
longPressRecognizer.minimumPressDuration = 0.5
longPressRecognizer.delegate = self
mapView.addGestureRecognizer(longPressRecognizer)

mapView.isMyLocationEnabled = true
mapView.settings.compassButton = true

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

extension DashedOverlayViewController : UIGestureRecognizerDelegate
{
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return true
}
}

Swift 5: How to add Marker on GoogleMap on tap/touch with coordinates?

For Swift 5.0+

First, make sure you have added GMSMapViewDelegate delegate to your ViewController Class

We do not need UILongPressGestureRecognizer or UITapGestureRecognizer for this, GMSMapViewDelegate provides convenient default method for this.

///This default function fetches the coordinates on long-press on `GoogleMapView`
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {

//Creating Marker
let marker = GMSMarker(position: coordinate)

let decoder = CLGeocoder()

//This method is used to get location details from coordinates
decoder.reverseGeocodeLocation(CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) { placemarks, err in
if let placeMark = placemarks?.first {

let placeName = placeMark.name ?? placeMark.subThoroughfare ?? placeMark.thoroughfare! ///Title of Marker
//Formatting for Marker Snippet/Subtitle
var address : String! = ""
if let subLocality = placeMark.subLocality ?? placeMark.name {
address.append(subLocality)
address.append(", ")
}
if let city = placeMark.locality ?? placeMark.subAdministrativeArea {
address.append(city)
address.append(", ")
}
if let state = placeMark.administrativeArea, let country = placeMark.country {
address.append(state)
address.append(", ")
address.append(country)
}

// Adding Marker Details
marker.title = placeName
marker.snippet = address
marker.appearAnimation = .pop
marker.map = mapView
}
}
}

HOPE IT HELPS !!!

How to add marker for a map view in google maps sdk for ios in swift

When performing UI Updates in closures(In my case - Plotting markers),Do remember to get main thread and perform UI Operations on main thread only.

Mistake what i did is,I'm trying to plot markers in web service completion block.

dispatch_async(dispatch_get_main_queue(),
{
var position = CLLocationCoordinate2DMake(17.411647,78.435637)
var marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = vwGogleMap
})

// For swift 3.0 support.
// 1. Get Main thread
DispatchQueue.main.async
{
// 2. Perform UI Operations.
var position = CLLocationCoordinate2DMake(17.411647,78.435637)
var marker = GMSMarker(position: position)
marker.title = "Hello World"
marker.map = vwGoogleMap
}

Hope this helps for someone!

Implementing gesture on a marker.iconView in swift 3 and google maps for ios

The way I approached it was creating a custom GMSMarker. This will create the points on your mapView.

You want to pass in your sportEvent object when initializing the custom marker (i.e. something like CustomMarker(with: sportEvent))

The GMSMarker object has some properties you can set which you can find here but, some of the notable ones include:

title, snippet (text beneath title) which is what you want to set from your sportEvent object.

So, in your custom marker you can do:

Class CustomMarker: GMSMarker {
var title: String
var desc: String
...

init(with sportEvent: SportEvent) {
// do some validation before setting this assumes all values are set correctly
self.title = sportEvent.title //(assuming `.title` is a string)
self.desc = sportEvent.description
...
super.init()

title = self.title
snippet = self.desc
}
}

Note that the custom GMSMarker may need a position property set which is just a CLLocationCoordinate2D i.e. (position = CLLocationCoordinate2D(latitude: sportEvent.latitude, longitude: sportEvent.longitude)

This will by default show you a mini marker info window with your sportEvent object.

If you want to further customize the marker info window you can take a look at markerInfoWindow

It returns a view if it exists. What I did was create my view programatically and I passed in my custom marker into the view and set everything accordingly there.

Like so:

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let customMarker = marker as! CustomMarker
return myCustomView(with: customMarker)
}

Where myCustomView is a method that returns a UIView`

func myCustomView(with customMarker: CustomMarker) -> UIView {
// Set view programatically here with your customMarker
}

Hope it helps!



Related Topics



Leave a reply



Submit