gogeolocationgomobile

Access geolocation data using gomobile


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.


Solution

  • 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).