Access geolocation data using gomobile

那年仲夏 提交于 2021-02-08 09:28:44

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/45130826/access-geolocation-data-using-gomobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!