how can I multiple tapping with single marker - ios

by tapping, it makes marker infinity. i want to one marker where I tapped. over and over again. not with lots markers.
I used MapView.clean(). but it delete every markers.
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
{
let marker = GMSMarker(position: coordinate)
marker.position.latitude = coordinate.latitude
marker.position.longitude = coordinate.longitude
print("hello")
print(markerr.position.latitude)
let ULlocation = markerr.position.latitude
let ULlgocation = markerr.position.longitude
print(ULlocation)
print(ULlgocation)
marker.map = self.mapView
}

Create the marker outside the didTapAt coordinate method and change it's coordinates in this method
class ViewController: UIViewController, GMSMapViewDelegate {
let marker = GMSMarker()
override func viewDidLoad() {
super.viewDidLoad()
marker.map = self.mapView
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
{
marker.position = coordinate
}
}

Related

How to set fixed Marker position in Google MapView iOS

Here I'm working on mapKitView using GoogleMapKit and here i want to do when the user drag the marker by using of didBeginDragging, didDragging and didEndDragging
and release the marker at any location point of the screen. I found a helping content to fixed the marker in screen from Hugo Jordao's answer, but this only works to fixed marker position in the center of the screen only.So how can i set marker at any specific screen point??
Dragging Method
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
if marker == mapMarker{
self.markerPosition = GMSCameraPosition(latitude: marker.position.latitude, longitude: marker.position.longitude, zoom: 5.0)
print("End Dragging")
}
}
Set Marker center to the Screen
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if self.markerPosition == nil{
mapMarker.position = position.target
}else{
mapMarker.position = self.markerPosition.target
}
}
Answering my own question.
First added variable to store the location value in CGPoint
var markerPosition : CGPoint!
Then convert CLLocationCoordinate2D Location values into the CGPoint and store into markerPosition in didEndDragging marker method.
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
if marker == mapMarker{
self.markerPosition = self.mapView.projection.point(for: CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude))
print("End Dragging")
}
}
After that set GMSMarker position at any screen point as per CGPoint values into the MAPView
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if self.markerPosition == nil{
//To set pin into the center of mapview
mapMarker.position = position.target
}else{
//To set pin into the any particular point of the screen on mapview
let coordinate = self.mapView.projection.coordinate(for: self.markerPosition)
mapMarker.position = coordinate
}
}

swift google maps will not show snippet window

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.

How do you set the delegate when passing data from ViewController to View?

Here's the relevant code:
in ViewController
protocol LocationDelegate {
func setLocation(coordinate: CLLocationCoordinate2D)
}
var locationDelegate: LocationDelegate?
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
locationDelegate?.setLocation(coordinate: coordinate)
createPostView = createViewFromNib()
createPostView.center = SCREEN_CENTER_POSITION
self.view.addSubview(createPostView)
}
in CreatePostView
class CreatePostView: UIView, UINavigationControllerDelegate, LocationDelegate {
var location: CLLocation! = CLLocation()
func setLocation(coordinate: CLLocationCoordinate2D) {
self.location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
}
This doesn't work. "location" is always being saved as empty and I believe it's because I haven't set the delegate. I know this is usually done in prepareForSegue when passing data between two ViewControllers, but in this case, I'm not sure when to set it. How should I go about this?
I think you are confused about how the delegate pattern works.
If I understand what you are trying to do... In ViewController you are accepting a longPress on the mapView, which is also passing the CLLocationCoordinate2D. You then create a CreatePostView which you want to add to your view as a subview... and you want to set the location var in that createPostView instance to the long-press coordinate. Correct?
If so, you don't need the delegate at all.
Instead, your CreatePostView class should have:
class CreatePostView: UIView, UINavigationControllerDelegate {
var location: CLLocation! = CLLocation()
func setLocation(coordinate: CLLocationCoordinate2D) {
self.location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
}
}
and your ViewController class should have:
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
// instantiate your CreatePostView
createPostView = createViewFromNib()
// set it's .center property
createPostView.center = SCREEN_CENTER_POSITION
// here is where you "pass in" the coordinate
createPostView.setLocation(coordinate: coordinate)
// add it to your view
self.view.addSubview(createPostView)
}
You would want to use a delegate pattern if, for example, your createPostView had text fields and buttons, and you want to pass those values "up" to your ViewController.
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
createPostView = createViewFromNib() as! CreatePostView
createPostView.setLocation(coordinate: coordinate)
createPostView.center = SCREEN_CENTER_POSITION
self.view.addSubview(createPostView)
}

change print text when tap in GMSPoligon. Swift

I have some GMSPoligon:
let españa = GMSMutablePath()
españa.add(CLLocationCoordinate2D(latitude: 42.11, longitude: -9.37))
españa.add(CLLocationCoordinate2D(latitude: 43.94, longitude: -9.55))
españa.add(CLLocationCoordinate2D(latitude: 43.60, longitude: -1.89))
españa.add(CLLocationCoordinate2D(latitude: 42.02, longitude: 3.72))
españa.add(CLLocationCoordinate2D(latitude: 36.16, longitude: -2.65))
españa.add(CLLocationCoordinate2D(latitude: 37.10, longitude: -7.28))
españa.add(CLLocationCoordinate2D(latitude: 42.08, longitude: -6.61))
let polygonEspaña = GMSPolygon(path: españa)
polygonEspaña.fillColor = UIColor(red : CGFloat(fillColorRed), green: CGFloat(fillColorGreen), blue: CGFloat(fillColorBlue), alpha: CGFloat(fillColorAlpha));
polygonEspaña.strokeColor = strokeColor
polygonEspaña.strokeWidth = strokeWidth
polygonEspaña.map = mapView
polygonEspaña.isTappable = true
I implemented some delegates:
import UIKit
import GoogleMaps
class MapViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
and some functions
// ###### Click Country
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
print("User Tapped Layer: \(overlay)")
// FUNCTION THAT IS NOT WORKING: print("has clicado en : (GMSPoligon)")
}
// #### Find location in console.
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
}
GMSPolygon works and they are visible on the map, but when i click them. i recieve this text on my console:
LOCATION TAP : You tapped at 52.8075964305343, 16.8773408979177
GMSPoigonTap: User Tapped Layer: (null) size 7 color UIExtendedSRGBColorSpace 0.25 0 0 0.05
I want to change this text and recive, the county. if i tap in spain: YOU CLICKED ON SPAIN. if i click on Germany : YOU CLICKED ON GERMANY.
How can i do that?
Thanks at all buddies.
Finally i added this property to my GMSPolygon
polygonEspaña.title = "España"
And this function.
func mapView(_ mapView: GMSMapView, didTap overlay: GMSOverlay) {
print("\(overlay.title)"
Now i receive in my console : Optional("España"

drop a pin when user touch screen in google maps swift

I want to add and remove Marker(pin) in Google Maps.
I want to drop pin with long touch and remove it. I want to use it to select my destination. How can I do it?
let position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.map = mapView
For the ones who're looking for a complete code snippet using Swift:
Implement Protocol GMSMapViewDelegate
Drag an Instance of GMSMapView #IBOutlet weak var googleMapView: GMSMapView!
Mention GMSMapView Delegate within viewDidLoad() as googleMapView.delegate = self
Implement the didTapAt delegate function:
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
googleMapView.clear() // clearing Pin before adding new
let marker = GMSMarker(position: coordinate)
marker.map = googleMapView
}
This code should help you!
//MARK: GMSMapViewDelegate Implimentation.
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
plotMarker(AtCoordinate: coordinate, onMapView: mapView)
}
//MARK: Plot Marker Helper
private func plotMarker(AtCoordinate coordinate : CLLocationCoordinate2D, onMapView vwMap : GMSMapView) {
let marker = GMSMarker(position: coordinate)
marker.map = vwMap
}
PS: Dont forget to confirm to GMSMapViewDelegate in ViewController and assign googleMap.delegate = self somewhere in viewDidLoad()
Hope that helps!

Resources