I assume you mean in the browser (not a native application), since you have tagged this question as javascript. You do it via an asynchronous callback:
//Function that does something once the device has a location
//May be called repeatedly
function handler(location) {
var longitude = location.coords.longitude;
var latitude = location.coords.latitude;
var accuracy = location.coords.accuracy
//Do something with them
}
//Register the handler and tell the phone to start finding a location
navigator.geolocation.getCurrentPosition(handler);
It is all part of the HTML5 geolocation stuff, there is a draft here.