Add offset to a specific location

Yair Carreno
2 min readJun 7, 2024

--

We may sometimes receive a request to calculate and add a location from another specific location, that is, to add an offset to a specific location.
That could be useful for calculating distances, simulating random points around a specific location, or any other purpose the application requires.

Offset from current location

The solution I’m sharing has been successfully implemented for Android and iOS clients. It’s equally applicable to other client types like the Web and can be easily translated into different languages.

Example taken from QrartePets

iOS approach

For iOS, we can implement a utility that receives the reference location, distance, and degrees of location concerning north and clockwise.

Example in iOS to create offset from a particular location

computeOffset is a method that will receive parameters such as reference length, distance in meters, and orientation in degrees.

For instance, if we want to simulate a location that is between 20 to 50 meters away and an orientation between 5 and 360 degrees, we could implement this:

Testing the method

Android approach

For Android, the implementation is similar, using as a reference a specific location, a distance to said location and an orientation in degrees.

But guess what? That already exists and is implemented in a utility 😂!

The open-source Maps SDK for Android Utility Library provides this and many other useful operations for calculating distances on maps.

Best of all, it also has a library for iOS (we love Swift ❤️, so no problem, Swifters).

The Maps SDK for Android Utility Library includes operations such as:

  • Add an offset to a specific location.
  • Determine the great circle distance between coordinates.
  • Determine if a coordinate is inside a polygon.
  • Determine if a coordinate lies on or near a path within a specified tolerance.
  • Determine the area of this polygon.

Setup in iOS: https://developers.google.com/maps/documentation/ios-sdk/utility/setup

Setup in Android: https://developers.google.com/maps/documentation/android-sdk/utility/setup

Conclusion

So, it’s your decision whether to keep things simple and implement only the operations you require or integrate the library to have all the operations.
However, I recommend you explore this open-source Google utility, which comes with many operations you might need for your projects.

--

--

Yair Carreno
Yair Carreno

Written by Yair Carreno

Software engineer with a technical blog about #iOS, #Android, #Angular. Author of “The Clean Way to Use Rx”: https://leanpub.com/the-clean-way-to-use-rx

No responses yet