GPS and HTML5 game [SOLVED]

HI !!

I am trying to get GPS coordinates using JS.
In the index.html file, I have added a function in JS that I am running in the project.
However, I am not getting the correct data. Why ?

My JS function

function getLocation() {
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
            return positionInfo;
        });
    } else {
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
}

Code in Defold

function on_input(self, action_id, action)
	if action_id == hash("touch") then
		if html5 then
			local ss = html5.run("getLocation()")
			print ("loc: " .. ss)
		end
	end
end

and what I see in the console logs

image

Is the geolocation code working outside of Defold for you?

You might want to use https://github.com/AGulev/jstodef to send Defold a message with the geolocation data once the async function is done.

2 Likes

Yes it works perfect outside of Defold.

Hey, your function is async witch means you can’t just return value as you do now.

function getLocation() {
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(YOUR_ASYNC_CALLBACK_HERE);
    } else {
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
}

Use JsToDef extension to get this info (there are docs and example in the repo)

It should be something like:

function getLocation() {
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
            JsToDef.send("PositionInfo", positionInfo);
        });
    } else {
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
}
5 Likes

Thank you very much for explaining

1 Like

It works great. Thank you so much, man.

1 Like

Sorry @AGulev , but I have more questions for html5 games and JS:

  1. Is it possible to run the fullscreen from game rather than clicking on the link ?
    I would like to use html5.run function.
  2. Is it possible to set fullscreen by default in HTML5 version
  1. Take a look DefOS extension. https://github.com/subsoap/defos
    It has functionality for toggle fullscreen from the game. Here is an example https://github.com/subsoap/defos/blob/8867999886025170264e518f9ea6751bfae2a2ab/example/example.gui_script#L87
    But! It has to be a click anyway. It is browsers security requirements.

  2. No, I afraid it’s impossible. Again, because of browsers security requirements.
    Element.requestFullscreen() - Web APIs | MDN

2 Likes