IBM Worklight - How to implement GPS functionality

2019-09-22 16:36发布

I am currently working on an Android app using Worklight.

I am not able to understand how to implement geolocation and how to call it from HTML button event.

2条回答
你好瞎i
2楼-- · 2019-09-22 17:29

Worklight 6.0 added Location Services APIs that go beyond the w3c geolocation APIs available in the navigator.geolocation object. To sum it up, they allow you to:

  1. Get the current location.
  2. Setup ongoing acquisition policy (desired accuracy, min time and distance between location updates, etc - things that influence the battery consumption).
  3. Setup location-change and geo-fence triggers (exit, enter, dwell inside or outside a polygon or a circle) which can
    1. trigger callback functions and
    2. send semantic location events to the server - which can act upon them using event handlers you register in your adapters.
  4. Do the same also for indoor location using wifi visibility (on Android) and connected wifi (on Android, iOS, and WP8).

As of 6.1 this is available also as native APIs for iOS and Android. We've also added some cool features that allow you to conveniently debug and test location-based hybrid (javascript) apps - see here.

查看更多
干净又极端
3楼-- · 2019-09-22 17:35

Take a look at the Apache Cordova documentation here and follow the examples. Under Advanced Topics read the Location Services PDF and Example Code here. There's also documentation in IBM Information Center here and API documentation here.

Quick example, run on a real device with the GPS turned on:

index.html

<button onclick="alertGeo()">Click to alert GPS info.</button>

main.js

function alertGeo() {

    navigator.geolocation.getCurrentPosition(onSuccess, onError);

    function onSuccess(position) {
      alert(JSON.stringify(position));
    }

    function onError(error) {
        alert(JSON.stringify(error));
    }
}
查看更多
登录 后发表回答