duanpei8853 2017-07-16 16:24
浏览 94
已采纳

使用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.

  • 写回答

1条回答 默认 最新

  • dougai3418 2017-07-16 21:24
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?