使用gomobile访问地理位置数据

huangapple go评论80阅读模式
英文:

Access geolocation data using gomobile

问题

我可以使用原生的gomobile应用程序访问手机的地理位置吗?

我在这里看到了对传感器数据的实验性支持(https://godoc.org/golang.org/x/mobile/exp/sensor),但没有关于设备的实际纬度和经度的信息。

我之前没有应用程序开发经验,目前正在学习Go语言。

英文:

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

得分: 3

据我所知,目前还没有现成的解决方案可以以平台无关的方式访问Android和iOS上的位置信息。但是理论上,你可以使用gomobile工具生成每个平台的绑定来创建单独的包。

例如,在Android上,你可以使用以下代码:

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

locationManager := ctx.GetSystemService(Context.LOCATION_SERVICE)
// 省略了空指针检查。
location := locationManager.GetLastKnownLocation(LocationManager.GPS_PROVIDER)
// 省略了空指针检查。
lat := location.GetLatitude()
lng := location.GetLongitude()

其中,ctx是你在生命周期回调中接收到的上下文(活动、服务等)。

此外,你还可以使用其他提供程序(如网络、融合)。

英文:

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

huangapple
  • 本文由 发表于 2017年7月17日 00:24:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/45130826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定