How to more efficiently get time zone based on current coordinates offline on Android?

80 Views Asked by At

I'm trying to get the time zone of the current latitude and longitude to use for time and daylight savings adjustments. Ideally I'll be able to do this without an internet connection.

My current implementation uses https://github.com/dustin-johnson/timezonemap which is the suggested library for an offline Android implementation based on this thread: How to get a time zone from a location using latitude and longitude coordinates?

It loads the timezone map for everywhere in a singleton on a background thread on app start up, and then accesses that same object whenever the user changes the saved coordinates. The object it creates is too big for some of the devices I'm testing on and the app crashes.

I also tried loading a timezone map for the current location, and retrieving it again every time the location is changed. There was about a 5 second delay to get the time zone, which made for a glitchy user experience.

1

There are 1 best solutions below

1
hcl2000 On

First I would examine the necessity of using GPS to derive coordinates. If you still find you need to do so, this thread may prove useful.

Java & Kotlin also have TimeZone.getDefault() which returns the system's default timezone. In this case, whatever the user has selected in their settings on the device. This is ideal for most use cases and far less complicated than deriving it from GPS, many devices do not have GPS capability anyways.