BBjGeolocation Example

rem ' geo.txt

rem ' See: http://mapki.com/wiki/Google_Map_Parameters
rem ' t=type (m=map, k=satellite, h=hybrid, p=terrain, e=Google Earth)

url$ = "http://maps.google.com/?t=m&output=embed&hl="+stbl("!LOCALE")
loc$ = "35.150036,-106.593957"; rem ' BASIS

rem ' Background information about the Geolocation spec
rem ' See: https://dev.w3.org/geo/api/spec-source.html
rem ' See: https://diveintohtml5.org/geolocation.html
rem ' See: http://benwerd.com/lab/geo.php

precision 6
sysgui = unt
open (sysgui)"X0"
sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(10,10,800,500,"BBj BUI Geolocation",$00090003$)
window!.setCallback(window!.ON_CLOSE,"eoj")
getCurrentPosition! = window!.addButton(1,10,10,100,25,"Get Location",$$)
getCurrentPosition!.setCallback(getCurrentPosition!.ON_BUTTON_PUSH,"getCurrentPosition")
window!.addStaticText(199,250,15,40,25,"URL:",$8000$)
url! = window!.addEditBox(99,300,10,490,25,url$,$$)
highAccuracy! = window!.addCheckBox(100,120,10,120,25,"High Accuracy",$0004$)
window!.addStaticText(201,10,45,100,25,"Max Age (seconds):",$8000$)
maximumAge! = window!.addInputN(101,120,40,100,25,$$,"###,###.000",$$,300,300)
window!.addStaticText(202,10,75,100,25,"Timeout (seconds):",$8000$)
timeout! = window!.addInputN(102,120,70,100,25,$$,"###,###.000",$$,60,60)
window!.addStaticText(203,10,105,100,25,"Status:",$8000$)
status! = window!.addStaticText(103,120,105,170,25,"",$$)
tooltip$ = "(0=Success, 1=Permission denied, 2=Position unavailable, 3=Timeout)"
status!.setToolTipText(tooltip$)
window!.addStaticText(204,10,135,100,25,"Message:",$8000$)
message! = window!.addStaticText(104,120,135,170,25,"",$$)
window!.addStaticText(205,10,165,100,25,"Timestamp:",$8000$)
timestamp! = window!.addStaticText(105,120,165,170,25,"",$$)
window!.addStaticText(206,10,195,100,25,"Latitude:",$8000$)
latitude! = window!.addStaticText(106,120,195,170,25,"",$$)
window!.addStaticText(207,10,225,100,25,"Longitude:",$8000$)
longitude! = window!.addStaticText(107,120,225,170,25,"",$$)
window!.addStaticText(208,10,255,100,25,"Accuracy:",$8000$)
accuracy! = window!.addStaticText(108,120,255,170,25,"",$$)
window!.addStaticText(209,10,285,100,25,"Altitude:",$8000$)
altitude! = window!.addStaticText(109,120,285,170,25,"",$$)
window!.addStaticText(210,10,315,100,25,"Altitude Accuracy:",$8000$)
altitudeAccuracy! = window!.addStaticText(110,120,315,170,25,"",$$)
window!.addStaticText(211,10,345,100,25,"Heading:",$8000$)
heading! = window!.addStaticText(111,120,345,170,25,"",$$)
window!.addStaticText(212,10,375,100,25,"Speed:",$8000$)
speed! = window!.addStaticText(112,120,375,170,25,"",$$)
watchPosition! = window!.addButton(113,10,400,100,25,"Watch Position",$$)
watchPosition!.setCallback(getCurrentPosition!.ON_BUTTON_PUSH,"watchPosition")
watchPositionMessage! = window!.addStaticText(213,120,405,170,25,"",$$)
clearWatch! = window!.addButton(114,10,430,100,25,"Clear Watch",$$)
clearWatch!.setEnabled(0)
clearWatch!.setCallback(getCurrentPosition!.ON_BUTTON_PUSH,"clearWatch")
clearWatchMessage! = window!.addStaticText(214,120,435,170,25,"",$$)
map! = window!.addHtmlView(999,300,40,490,450,"")
geolocation! = sysgui!.getGeolocation(err=unavailable)
dim z[0:21]
m = 10; for z = 21 to 0 step -1; z[z] = m; m = m * 2; next z

process_events

unavailable:
    i = msgbox("Geolocation is not available.",16,"Unavailable")

eoj:
release

getCurrentPosition:
    geolocation!.setCallback(geolocation!.ON_GEOLOCATION_POSITION,"geo")
    geolocation!.setHighAccuracy(highAccuracy!.isSelected())
    geolocation!.setMaximumAge(maximumAge!.getValue())
    geolocation!.setTimeout(timeout!.getValue())
    geolocation!.getCurrentPosition()
return

watchPosition:
    geolocation!.setCallback(geolocation!.ON_GEOLOCATION_WATCH,"geo")
    geolocation!.setHighAccuracy(highAccuracy!.isSelected())
    geolocation!.setMaximumAge(maximumAge!.getValue())
    geolocation!.setTimeout(timeout!.getValue())
    watchPositionMessage!.setText("Watching position...")
    watchPosition!.setEnabled(0)
    clearWatch!.setEnabled(1)
return

clearWatch:
    geolocation!.clearCallback(geolocation!.ON_GEOLOCATION_WATCH)
    clearWatchMessage!.setText("Stopped watching position.")
    watchPosition!.setEnabled(1)
    clearWatch!.setEnabled(0)
return

geo:
    geo! = sysgui!.getLastEvent()
    url$ = url!.getText()

    rem ' status (0, 1, 2, 3)
    val! = geo!.getStatus()
    switch val!
        case 0; status$ = "Success"; break
        case 1; status$ = "Permission denied by user"; break
        case 2; status$ = "Position unavailable"; break
        case 3; status$ = "Timeout"; break
        case default; status$ = "Unknown code "+str(val!); break
    swend
    status!.setText(status$)

    rem ' message (error message or null)
    val! = geo!.getMessage()
    if val! = null()
        message!.setText("")
    else
        message!.setText(str(val!))
    endif

    rem ' timestamp (may be null)
    val! = geo!.getTimestamp()
    if val! = null()
        timestamp!.setText("N/A")
    else
        date! = new java.util.Date(val!.longValue())
        timestamp!.setText(str(date!))
    endif

    rem ' latitude
    val! = geo!.getLatitude()
    if val! = null()
        latitude!.setText("N/A")
    else
        latitude!.setText(str(val!))
    endif

    rem ' longitude
    val! = geo!.getLongitude()
    if val! = null()
        longitude!.setText("N/A")
    else
        longitude!.setText(str(val!))
    endif

    rem ' location accuracy, measured in metres
    val! = geo!.getAccuracy()
    if val! = null()
        accuracy = 99999999
        accuracy!.setText("N/A")
    else
        accuracy = val!
        feet = round(val!*3.2808399,0)
        accuracy!.setText(str(val!)+" metres ("+str(feet)+" feet)")
    endif

    rem ' altitude in metres
    val! = geo!.getAltitude()
    if val! = null()
        altitude!.setText("N/A")
    else
        feet = round(val!*3.2808399,0)
        altitude!.setText(str(val!)+" metres ("+str(feet)+" feet)")
    endif
    val! = geo!.getAltitudeAccuracy()
    if val! = null()
        altitudeAccuracy!.setText("N/A")
    else
        feet = round(val!*3.2808399,0)
        altitudeAccuracy!.setText(str(val!)+" metres ("+str(feet)+" feet)")
    endif
    val! = geo!.getHeading()
    if val! = null()
        heading!.setText("N/A")
    else
        heading!.setText(str(val!)+" degrees")
    endif
    val! = geo!.getSpeed()
    if val! = null()
        speed!.setText("N/A")
    else
        kmh = round(val!*3.6,0)
        mph = round(val!*2.2369362920544,0)
        speed!.setText(str(val!)+" m/s ("+str(kmh)+" km/h; "+str(mph)+" mph)")
    endif
    if geo!.getLatitude()<>null() and geo!.getLongitude()<>null
        loc$ = str(geo!.getLatitude())+","+str(geo!.getLongitude())
    endif
    for z = 21 to 1 step -1
        if z[z] > accuracy then break
    next z
    window!.setTitle("BBj BUI Geolocation: "+loc$)
    map!.setUrl(url$+"+&z="+str(z)+"&q="+loc$)
return

See Also

BBjAPI

BBjSysGui

BBj Object Variables

BBj Object Assignment

BBj Object Error Handling

BBj Object Operators

Geolocation API Specification

BBjGeolocation

BBj Object Diagram for an illustration of the relationship between BBjObjects.