Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Geolocation API - getCurrentPosition 1

Status
Not open for further replies.

slobad23

IS-IT--Management
Jun 30, 2006
90
GB
The current script works:

<code>
window.onload = init;

function init() {

var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + new Date(position.timestamp) + '\n');
};

// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}

navigator.geolocation.getCurrentPosition(onSuccess, onError);

};
</code>

What I don't understand is how the getCurrentPosition function provides onSuccess with an object to the parameter. How is the position parameter given an object to work with by getCurrentPosition?

I am not sure how I would do this with creating my own functions - perhaps a simple piece of code that shows this working would be what I need.

I have searched for this and have been unable to find the answer.

Thanks,
 
What I don't understand is how the getCurrentPosition function provides onSuccess with an object to the parameter. How is the position parameter given an object to work with by getCurrentPosition?

No way to know since we don't know what getCurrentPosition actually does.


As for on success, I don't see it getting any value. The function merely alerts all the data from the position object.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
From my understanding, position is passed to the onSuccess function as a parameter value.

What I don't understand is how a function calls another function provide that second function with an object to use.

getCurrentPosition(onSuccess) // the parameter is a function
onSuccess(position) // position comes from getCurrentPosition

How does position get passed to onSuccess to be used as the parameter?

If it is just a case of "Who knows... just accept it" then I will go with that. Otherwise, I would just like to understand it a little more.


 
From my understanding, position is passed to the onSuccess function as a parameter value.
It kind of is. Its an expected parameter of the nameless function being assigned to the onSuccess <b>variable</b>

What I don't understand is how a function calls another function provide that second function with an object to use.

getCurrentPosition(onSuccess) // the parameter is a function
onSuccess(position) // position comes from getCurrentPosition

How does position get passed to onSuccess to be used as the parameter?

No way to say for sure, since we don't know what the getCurrentPosition() function is actually doing.

As a guess It could be doing something like:

Code:
function getCurrentPosition(funcX, FuncY){

.... code to get geo position  ....

position = [object generated from geo position code];
[b]funcX(position);[/b][green]//getCurrentPosition uses the functions passed to it and provides it with a variable. [/green]
}

Here's a working example.
OnSuccess is assigned a nameless function which alerts the value passed to it. The second function takes that variable and uses the function stored with in it, and passes a value generated inside the second function to it.

Code:
<script type="text/javascript">

var [red]onSuccess[/red]=function(myvar){
alert(myvar);
}

function myfunc([blue]theVarFunc[/blue]){

[blue]theVarFunc[/blue]('This will get alerted by onSuccess');

}

myfunc([red]onSuccess[/red]);


</script>

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top