Profile picture

Muhammad Zeeshan

Yeah! it's me ;)

GMSGeocoder cancel ongoing request workaround

{"alt" : "Geocoding","width": 600,"height": 400,"thumb": "https://dzm9gzl83npws.cloudfront.net/thumbnail_geojango_maps_Z8_Ug_B80_46w_unsplash_6923591ac2.jpg"}

Reverse GeoCoding:

The process of converting latitude and longitude into human readable address is called Reverse GeoCoding.

If you are developing an iOS app in which app allows its user to select the location on map. Then surely you will need to convert the selected latitude and longitude into human readable address by using reverse geocoding, in order to show what address does user has selected. The code for reverse geocoding is pretty straight forward.

 

In case if you want to use Google's GMSGeocoder then most probably you will run into a situation where you want to entertain the last request only. Didn't understand what I mean? Lets consider the following scenario:

  • Your app allows its user to drag the map view in order to change the camera focus.
  • This process can't be blocking for better user experience.
  • User drag to change the camera focus.
  • When focus did change your app send the reverse geocoding request.
  • Before the request could complete, user change the camera focus again.
  • Your app send the new reverse geocoding request.

As the requests are asynchronous, this means there is no guarantee which request will be completed first. So, in this particular case you need to cancel the outstanding reverse geocoding requests before making new requests. But guess what? there is no cancel method for GMSGeocoder.

 

image in an article

 

Luckily this can be solved by adding 1 simple logic, found here. We can modify our reverse geocoding helper function to entertain the last request only and ignore all outstanding stale requests. Here is the code.

 

Enjoy!

More To Read

cocoapods_custom_script_24539e2b8a.jpg
iOSWriting custom script for Cocoapods

Cocoapods is a popular dependency manager among iOS developers. iOS developers often use Cocoapods for integrating third party libraries. It provides different hooks to perform certain custom actions.

avatar_me.jpeg

Muhammad Zeeshan

Want to get in touch? Feel free to contact me at

mzeeshanid@yahoo.com

Muhammad Zeeshan

Copyright © 2021