1

Can I access the phone's geolocation using a native gomobile application?

I have seen experimental support for sensor data here, but nothing about the actual latitude, longitude of the device.

I have no prior experience in app development, and I am learning Go at the moment.

adrpino
  • 960
  • 8
  • 33

1 Answers1

3

AFAIK there is no ready solution yet to access location on both Android and iOS in a platform independent way. But in theory you could make separate packages using the gomobile tool to generate bindings for each platform.

For example on Android you would use something like:

import "Java/android/content/Context"
import "Java/android/location/LocationManager"

locationManager := ctx.GetSystemService(Context.LOCATION_SERVICE)
// nil check omitted.
location := locationManager.GetLastKnownLocation(LocationManager.GPS_PROVIDER)
// nil check omitted.
lat := location.GetLatitude()
lng := location.GetLongitude()

Where ctx is the context (an activity, service, etc.) that you receive in one of their lifecycle callbacks.

Also you can use other providers (network, fused).

dev.bmax
  • 8,998
  • 3
  • 30
  • 41
  • 1
    out of curiosity - where these imports `Java/android/content/Context` and `Java/android/location/LocationManager` are coming from? – jdevelop Dec 13 '20 at 04:00