iOS : Swift - How to add pinpoint to map on touch and get detailed address of that location?

add multiple annotations to mapview ios swift
swift mapkit annotation
swift mapkit current location
mapkit tutorial swift 4
mkmarkerannotationview
mkpointannotation
how to search for location and display results using apple’s mapkit swift
custom annotation view swift

I want to add annotation on touch of iOS map and fetch the detailed address (Placemark) of respective location. How I can achieve this in Swift?

Thanks in advance.

To react to the touch on map you need to set up a tap recogniser for the mapView

in viewDidLoad:

let gestureRecognizer = UITapGestureRecognizer(target: self, action:"handleTap:")
    gestureRecognizer.delegate = self
    mapView.addGestureRecognizer(gestureRecognizer)

Handle the tap and get the tapped location coordinates:

func handleTap(gestureRecognizer: UILongPressGestureRecognizer) {

    let location = gestureRecognizer.locationInView(mapView)
    let coordinate = mapView.convertPoint(location, toCoordinateFromView: mapView)

    // Add annotation:
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    mapView.addAnnotation(annotation)
}

Now you only have to implement the MKMapView delegate functions to draw the annotations. A simple google search should get you the rest of that.

MapKit Tutorial: Getting Started, I want to add annotation on touch of iOS map and fetch the detailed address (​Placemark) of respective location. How I can achieve this in Swift? Thanks in  36 How to set a specific default time for a date picker in Swift Oct 29 '15 23 iOS : Swift - How to add pinpoint to map on touch and get detailed address of that location? Dec 23 '15

Here is a working Xcode 10.1, Swift 4.2 project with MapKit delegates to control the annotations (pin color, pin image etc.) and delegate to handle a click on the added annotation: Github Project

import UIKit
import MapKit

class ViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.delegate = self
    let longTapGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
    mapView.addGestureRecognizer(longTapGesture)
}

@objc func longTap(sender: UIGestureRecognizer){
    print("long tap")
    if sender.state == .began {
        let locationInView = sender.location(in: mapView)
        let locationOnMap = mapView.convert(locationInView, toCoordinateFrom: mapView)
        addAnnotation(location: locationOnMap)
    }
}

func addAnnotation(location: CLLocationCoordinate2D){
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "Some Title"
        annotation.subtitle = "Some Subtitle"
        self.mapView.addAnnotation(annotation)
}
}

extension ViewController: MKMapViewDelegate{

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard annotation is MKPointAnnotation else { print("no mkpointannotaions"); return nil }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView

    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.rightCalloutAccessoryView = UIButton(type: .infoDark)
        pinView!.pinTintColor = UIColor.black
    }
    else {
        pinView!.annotation = annotation
    }
    return pinView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    print("tapped on pin ")
}

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        if let doSomething = view.annotation?.title! {
           print("do something")
        }
    }
  }
}

How to search for location and display results using Apple's MapKit, How do I search locations and display results using Apple's MapKit Swift? With Maps you can get directions with real-time traffic information, explore new places, save your favorite locations, and more. Maps makes things easier to find by giving you detailed information and suggestions based on your habits and calendar events. To add frequent locations, like your home or work: Open Maps, and tap in the top-right corner.

Swift 4:

 @IBOutlet weak var mapView: MKMapView!

 func handleLongPress (gestureRecognizer: UILongPressGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizerState.began {
        let touchPoint: CGPoint = gestureRecognizer.location(in: mapView)
        let newCoordinate: CLLocationCoordinate2D = mapView.convert(touchPoint, toCoordinateFrom: mapView)
        addAnnotationOnLocation(pointedCoordinate: newCoordinate)
    }
}

func addAnnotationOnLocation(pointedCoordinate: CLLocationCoordinate2D {
    let annotation = MKPointAnnotation()
    annotation.coordinate = pointedCoordinate
    annotation.title = "Loading..."
    annotation.subtitle = "Loading..."
    mapView.addAnnotation(annotation)
}

Add custom pin on map-view or Map-kit (Swift 4 + Xcode 9.2 , Since the map and table view are in two separate like a coordinate and address information. Type “Target” into the search bar and tap on one In ViewController.swift, add the following class extension: I can get the lat/long for the users location but  Use a dropped pin: Touch and hold any location on the map to drop a pin. Or, you can drag or tap the bottom-right corner of the screen, then tap Drop Pin. A pin drops on the map. Touch and hold the pin, then drag it to any location you choose. Getting Directions You can get step-by-step driving, public transit, or walking directions.

For Swift 4 I converted the Swift 3 example as the other Swift 4 one did not work for me: (note I'm using 'mapview' instead of 'mapView'just to fit in with other code

        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
        gestureRecognizer.delegate = self
        mapview.addGestureRecognizer(gestureRecognizer)


@objc func handleTap(_ gestureReconizer: UILongPressGestureRecognizer)
    {

        let location = gestureReconizer.location(in: mapview)
        let coordinate = mapview.convert(location,toCoordinateFrom: mapview)

        // Add annotation:
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinate
        mapview.addAnnotation(annotation)
    }

Swift iOS MapKit, We will learn how to add pins(annotations) on mapview using Mapkit framework. We will set Duration: 14:46 Posted: Mar 4, 2018 Select the Type button to display the satellite view and then zoom in to get a better look at the region: Figure 80-4 To get real location information, load the application onto a physical iOS device. Updating the Map View based on User Movement

For swift 3.0

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
gestureRecognizer.delegate = self
mapView.addGestureRecognizer(gestureRecognizer)

func handleTap(_ gestureReconizer: UILongPressGestureRecognizer) {

    let location = gestureReconizer.locationInView(mapView)
    let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)

    // Add annotation:
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    mapView.addAnnotation(annotation)
}

Centering the Map with MapKit and Core Location, Getting Started in Animation with Paul Trani - 1 of 2 Adobe Creative Cloud 816 watching. Live Duration: 6:53 Posted: Jul 31, 2018 If you touch the latitude and longitude coordinates on the Compass app display, iOS Maps will launch and show you that location. Part one solved. 2. Find LAT/LONG on a Map. Google explains how to

Adding a Map with a Marker | Maps SDK for iOS, Tap to unmute You can add annotations and overlays to the map to call out points of Duration: 5:00 Posted: Jan 9, 2019 Google Maps SDK for iOS; Google Places API Web Service; Select Credentials under APIs & services in the left pane menu. Click Create credentials, and then click API key to create the key: Copy the key and click Close: You’ll use the key in a moment, but first you’ll add the actual Google Maps iOS SDK. Adding the SDK

Markers | Maps SDK for iOS, Understand the code; Next steps. This tutorial shows you how to add a Google map to your iOS app. The map includes a marker, also called a pin, to indicate a specific location. Show/Hide the Swift code for the map view controller. /* * Copyright 2020 Google Inc. All Get Started. For more details, see Get an API key. In the script I tell the mapping API to center on the given location, add a pin icon on the location, and a popup overlay displaying the address. I’m also using the extended mapping options so a user can toggle between map and satellite view. That means you can see the actual building you’re in for every appointment!

Intro to MapKit API and Add an Annotation on Map, Markers indicate single locations on the map. By default To add a marker, create a GMSMarker object that includes a position and title , and set its map . With a more detailed map, immersive 3D experiences, and easy access to your go‑to and must‑see destinations, Maps is better than ever at getting you where you need to go. And helping you find more to do and experience when you get there. Getting around doesn’t. get much easier. A new map rebuilt. from the ground up.

Comments
  • He specifically mentions "Placemark." I found this post because I was searching for how to get a Placemark from an Annotation. I was encouraged that this thread would answer that, because that's what the OP is asking for. But, in any case, an answer that only address half the question cannot be considered complete.
  • I've added an edit for swift 3 @Neo42. You'll be able to see it once it's peer reviewed
  • This "answer" doesn't answer the user's question. The OP asked how to get a Placemark.
  • This "answer" doesn't answer the user's question. The OP asked how to get a Placemark.
  • Please read the question carefully again and then think about it again. The Question is how to add an annotation on touch & get the address. My answer covers 50% of it. Just because it doesn't answer your question, doesn't mean it is false.
  • To quote the OP: "...fetch the detailed address (Placemark)" he's asking for a Placemark.
  • No, but it seems you are. Yes, he asked for two things: placing an Annotation and getting a Placemark, not just the detailed address. Not a single answer addresses the Placemark issue. Partial credit for partial answers.
  • I never said your answer was false, by the way. But a 50% answer is not an answer. If someone asked for directions from Berlin to Frankfurt and I said, "Go to the end of the street and turn left," I'd hardly be applauded for answering the original question.
  • This "answer" doesn't answer the user's question. The OP asked how to get a Placemark.
  • This "answer" doesn't answer the user's question. The OP asked how to get a Placemark.
  • This "answer" doesn't answer the user's question. The OP asked how to get a Placemark.