Using the Geolocation APIs

Before you begin, see Geolocation Functions.

Getting the current position

<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfset res = cfclient.geolocation.getOptions()>
<cfset curpos = cfclient.geolocation.getCurrentPosition(res)>
<cfset document.getElementById('result').innerHTML=document.getElementById('result1').innerHTML+"Latitude : #curpos.coords.latitude#"> 
<cfset document.getElementById('result').innerHTML=document.getElementById('result2').innerHTML+"Longitude : #curpos.coords.longitude#"> 
<cfset document.getElementById('result').innerHTML=document.getElementById('result3').innerHTML+"Altitude : #curpos.coords.altitude#"> 
<cfset document.getElementById('result').innerHTML=document.getElementById('result4').innerHTML+"Accuracy : #curpos.coords.accuracy#"> 
<cfset document.getElementById('result').innerHTML=document.getElementById('result5').innerHTML+"Altitude Accuracy : #curpos.coords.altitudeAccuracy#"> 
<cfset document.getElementById('result').innerHTML=document.getElementById('result6').innerHTML+"Heading : #curpos.coords.heading#">
<cfset document.getElementById('result').innerHTML=document.getElementById('result7').innerHTML+"Speed : #curpos.coords.speed#">
<cfset document.getElementById('result').innerHTML=document.getElementById('result8').innerHTML+"Timestamp : #curpos.timestamp#">
</cfclient>
 
<div id="result1"/>
<div id="result2"/>
<div id="result3"/>
<div id="result4"/>
<div id="result5"/>
<div id="result6"/>
<div id="result7"/>
<div id="result8"/>

Setting Geolocation options

<cfclientsettings enableDeviceAPI=true>
<cfclient>
 
<cfset res = cfclient.geolocation.getOptions()>
<cfset res.enablehighaccuracy=false>
<cfset res.maximumage=3000>
<cfset res.timeout=5000>
<cfset cfclient.geolocation.setOptions(res)>

</cfclient>

Monitoring (watching) the current position

<cfclientsettings enableDeviceAPI=true>
<cfclient>

<cfset res = cfclient.geolocation.getOptions()>
<cfset res.enableHighAccuracy=false>
<cfset res.maximumAge=1000>
<cfset res.timeout=2000>
 
<cfset watchId=cfclient.geolocation.watchPosition(callbackfunc,res)>
 
<cffunction access="public" name="callbackfunc" returntype="void" >
<cfargument name="position"> 
<cfset alert(iter(position))>
</cffunction>
</cfclient>

Get help faster and easier

New user?