-2

i can't add marker with long touch in google maps in swift i have try lots of codes but it doesn't work for me what should i do !?

override func viewDidLoad() {

    super.viewDidLoad()        
    GMSServices.provideAPIKey("AIzaSyBw95wEhcSiSBmPWuYkiK0_IBnZQK-Lm7I")


    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error" + error.description)
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations.last
    let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

    let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
                                                      longitude: userLocation!.coordinate.longitude, zoom: 16)
    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    mapView.settings.myLocationButton = true
    view = mapView

   let  position = CLLocationCoordinate2DMake(10, 10)
    let marker = GMSMarker(position: position)


    marker.opacity = 0.6
    marker.position = center
    marker.title = "Current Location"
    marker.snippet = ""
    marker.map = mapView
    //mapView.clear()
}

Ant help would be appreciate

Nirav D
  • 71,513
  • 12
  • 161
  • 183
P.Tb
  • 99
  • 1
  • 5
  • 15

2 Answers2

2

Have you try this. First you need to add long gesture on mapView like this

For Objective C

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.mapView addGestureRecognizer:self.longPress];

Now add this handleLongPress function

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        CGPoint longPressPoint = [recognizer locationInView:self.mapView];
        CLLocationCoordinate2D coordinate = [mapView.projection coordinateForPoint: point];
        //Now you have Coordinate of map add marker on that location
     }
}

For swift

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("handleLongPress:")))
self.mapView.addGestureRecognizer(longPressRecognizer)

Now add this handleLongPress function

@objc func handleLongPress(recognizer: UILongPressGestureRecognizer)
 {
    if (recognizer.state == UIGestureRecognizerState.began)
    {
        let longPressPoint = recognizer.location(in: self.mapView);
        let coordinate = mapView.projection.coordinate(for: longPressPoint )
        //Now you have Coordinate of map add marker on that location
        let marker = GMSMarker(position: coordinate)
        marker.opacity = 0.6
        marker.title = "Current Location"
        marker.snippet = ""
        marker.map = mapView
    }
}

Hope this will help you

Jeff
  • 3,829
  • 1
  • 31
  • 49
Nirav D
  • 71,513
  • 12
  • 161
  • 183
0

It's not a big deal for now by google map provide an override function with a long press. Here is an example code:

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
    let marker = GMSMarker(position: coordinate)
   marker.icon = UIImage(named: "ic_pin_marker") // custom your own marker
   marker.map = mapView
}
Leang Socheat
  • 1,156
  • 2
  • 12
  • 23