Nouf
Nouf

Reputation: 773

Getting Location Details In Google Map With Swift

my question have two part , part one I was wondering how to show still pin in google map and when the user click on map the pin will drop giving the lat and long

the second part to to convert this coordinate to give me the location name

Upvotes: 1

Views: 2113

Answers (1)

Shahzaib Qureshi
Shahzaib Qureshi

Reputation: 920

For the First part you can use the delegate function of the google map view "mapView:didTapAtCoordinate:"

In this delegate method you can add a marker to at the tapped location.

let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "your title"
marker.snippet = "your snippet"
marker.map = mapView

In the position of the marker you can give the coordinates you get in the above mentioned delegate method.

For the Second part you can use this piece of code to get address :

you will need to import core location :

import CoreLocation
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(currentLocation, completionHandler: {
        placemarks, error in

            if error == nil && placemarks.count > 0 {
                 placemarks.last as? CLPlacemark

            }
        })

currentLocation here will be the coordinates that you get from delegate method didTapAtCoordinate.

Upvotes: 1

Related Questions