We have seen the Ola or Uber map where the pin is located in middle of Google Map & we can Drag the Map or Zoom also after doing all this , map stop dragging or zooming then
it will show their actual address where the pin located. In iOS we can use Apple Map or Google Map so here I have taken the Example of Google Map.
Please follow this steps.
1 |
pod 'GoogleMaps' |
1 2 3 4 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { GMSServices.provideAPIKey("sgjsbgjkbsgjdbgjdbgdjgbdjgbdjk"); return true } |
Take the one UIView in UIViewController & set the leading , trailing , top , bottom constraints to super view , after that take one UIImageView in middle of UIView and set the constraints center Horizontal & center vertical.
1 2 |
@IBOutlet var mapView: UIView! @IBOutlet var pinImage: UIImageView! |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public var longitude:Double = 77.38066792488098 public var latitude:Double = 28.6517752463408 var GoogleMapView:GMSMapView! var geoCoder :CLGeocoder! override func viewDidLoad() { super.viewDidLoad() self.SetUpMap() geoCoder = CLGeocoder() } func SetUpMap(){ let camera = GMSCameraPosition.camera(withLatitude:self.latitude, longitude: self.longitude, zoom: 12) GoogleMapView = GMSMapView.map(withFrame: CGRect(0, 0, SCREEN_WIDTH, self.mapView.frame.height), camera: camera) GoogleMapView.delegate = self self.mapView.addSubview(GoogleMapView) self.mapView.bringSubviewToFront(pinImage) } |
Note: it will show the PIN image on your given Latitude & Longitude position.
Now I have to implement the dragging functionality for this you need to write the delegate method to check when the map is at idle position after dragging. here i have used the geocode for getting the address through latitude & longitude.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) { let lat = position.target.latitude let lng = position.target.longitude // Create Location let location = CLLocation(latitude: lat, longitude: lng) // Geocode Location geoCoder.reverseGeocodeLocation(location) { (placemarks, error) in if let placemarks = placemarks{ if let location = placemarks.first?.location{ //self.addressTextField.text = (placemarks.first?.name ?? "")+" "+(placemarks.first?.subLocality ?? " ") if let addressDict = (placemarks.first?.addressDictionary as? NSDictionary){ let dict = JSON(addressDict) self.cityTextField.text = dict["City"].stringValue var address:String = "" for data in dict["FormattedAddressLines"].arrayValue{ address = address+" "+data.stringValue } // here you will get the Address. } } } } } |
Note: Now you can get the PIN position Address.