Geolocation: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
JwellsNB (talk | contribs)
Add javascript snippet
Line 37: Line 37:
End Function
End Function


navigator.geolocation.getCurrentPosition(handler);</pre>
navigator.geolocation.getCurrentPosition(handler);
</pre>
 
== Example (JavaScript) ==
<pre>
// Geolocation
 
function handler(location) {
  var s;
  NSB.Print("Longitude: " + location.coords.longitude);
  NSB.Print("Latitude: " + location.coords.latitude);
  NSB.Print("Accuracy: " + location.coords.accuracy);
  s = "<img src='http://maps.google.com/maps/api/staticmap?center=" +  location.coords.latitude + "," + location.coords.longitude +  "&zoom=14&size=300x200&maptype=roadmap&sensor=false'>";
  NSB.Print(s);
}
 
navigator.geolocation.getCurrentPosition(handler);
</pre>


== Output ==
== Output ==

Revision as of 19:49, 19 May 2013

NSB/App Studio programs run within a special version of a web browser. As a result, they can inherit a lot of the properties of the environment they run in. You can access these properties from within your app.

It is not a complete list: there are additional items in the full HTML documentation. In addition, some browsers may have additional members not shown here.

navigator.geolocation.getCurrentPosition(showMap)

showMap is the name of a function in your program, which will be called by the above statement. It is passed a single parameter: an object with the following fields:

geolocation object members

location.coords.longitude The current longitude of the device. GPS required.
location.coords.latitude The current latitude of the device. GPS required.
location.coords.accuracy The accuracy of the location. GPS required.

For more information on Google Maps, read http://code.google.com/apis/maps/documentation/staticmaps/.

Example

REM Geolocation
Function handler(location)
  Dim s
  Print "Longitude: " + location.coords.longitude 
  Print "Latitude: " + location.coords.latitude 
  Print "Accuracy: " + location.coords.accuracy
  s = "<img src='http://maps.google.com/maps/api/staticmap?center=" & _
  location.coords.latitude & "," & location.coords.longitude & _
  "&zoom=14&size=300x200&maptype=roadmap&sensor=false'>"
  print s
End Function

navigator.geolocation.getCurrentPosition(handler);

Example (JavaScript)

// Geolocation

function handler(location) {
  var s;
  NSB.Print("Longitude: " + location.coords.longitude);
  NSB.Print("Latitude: " + location.coords.latitude);
  NSB.Print("Accuracy: " + location.coords.accuracy);
  s = "<img src='http://maps.google.com/maps/api/staticmap?center=" +  location.coords.latitude + "," + location.coords.longitude +  "&zoom=14&size=300x200&maptype=roadmap&sensor=false'>";
  NSB.Print(s);
}

navigator.geolocation.getCurrentPosition(handler);

Output

Longitude: -79.2098782
Latitude: 43.73776441
Accuracy: 100
(map of current location)