Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

How to get current GPS Position within a WebView

Camilo edited this page May 29, 2021 · 2 revisions

This is an example using the $geo.get action and $agent service.

Example JSON

{
    "$jason": {
        "head": {
            "actions": {
                "fetchGPS": {
                    "type": "$geo.get",
                    "options": {
                        "distance": "1000"
                    },
                    "success": {
                        "type": "$agent.request",
                        "options": {
                            "id": "$webcontainer",
                            "method": "receiveGPS",
                            "params": ["{{$jason.coord}}"]
                        },
                        "success": {
                            "type": "$util.alert",
                            "options": {
                                "title": "GPS Fetched",
                                "description": "LAT: {{$jason.lat}} LONG:{{$jason.long}}"
                            }
                        }
                    }
                }
            }
        },
        "body": {
            "background": {
                "type": "html",
                "url": "file://index.html",
                "action": {
                    "type": "$default"
                }
            }
        }
    }
}

Example HTML

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script type="text/javascript">
        const receiveGPS = (coords) => {
            console.log("GOT COORDS");
            const coordinates = coords.split(",");
            const gps = {
                lat: coordinates[0],
                long: coordinates[1]
            };

            const element = document.getElementById("gps-location");
            element.innerHTML = JSON.stringify(gps);

            // This is important to trigger the success in the callstack
            $agent.response(gps);
        };

        const fetchGPS = () => {
            console.log("FETCHING GPS");
            $agent.trigger("fetchGPS");
        };
    </script>
</head>
<body>
 THE GPS LOCATION IS <span id="gps-location"></span>
 <br>
 <button onClick="fetchGPS()">FETCH GPS</button>
</body>
</html>
Clone this wiki locally