1

FindMyLocation is defined as follows:

function FindMyLocation(callback) {
if (navigator.geolocation) {

    var locationMarker = null;
    if (locationMarker) {
        return;
    }
    navigator.geolocation.getCurrentPosition(
        function(position){
           pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
           map.setCenter(new google.maps.LatLng(position.coords.latitude,position.coords.longitude));
           addMarker(
            position.coords.latitude,
            position.coords.longitude,
            "You are here"
            );
            setTimeout(function(window) {
                callback.call(pos);
            }, 1000);
        },
        function (geoPositionError) {
        switch (geoPositionError.code) {
            case 0:
                alert('An unknown error occurred, sorry');
                break;
            case 1:
                alert('Permission to use Geolocation was denied');
                break;
            case 2:
                alert('Couldn\'t find you...');
                break;
            case 3:
                alert('The Geolocation request took too long and timed out');
                break;
            default:
        }
    },
     timeoutCallback, 10, {maximumAge:60000, timeout:50000, enableHighAccuracy:true}
    );

var positionTimer = navigator.geolocation.watchPosition(
    function( position ){
        updateMarker(
            locationMarker,
            position.coords.latitude,
            position.coords.longitude,
            "Updated / Accurate Position"
        );
    }
);
setTimeout(
    function(){
        // Clear the position watcher.
        navigator.geolocation.clearWatch( positionTimer );
    },
    (1000 * 60 * 5)
);

} else {
    alert("Your phone browser doesn't support geolocation.");
}
}
function timeoutCallback(){
   alert('Hi there! we are trying to locate you but you have not answered the security    question yet.\n\nPlease choose "Share My Location" to enable us to find you.');
 }

I have this code running inside my homepage:

FindMyLocation(function() {
 myPos = this;
});

When I console.log myPos inside the callback, I see the value. However, I want to be able to use myPos outside this callback in order to do a distance calculation. Right now, it won't work.

Please help.

3
  • @David: when I try to alert or console.log myPos (which is declared as a global), I get undefined. Any ideas? I'm not strong on callbacks and JavaScript for that matter.
    – Mina
    Aug 28, 2012 at 12:51
  • 1
    "However, I want to be able to use myPos outside this callback in order to do a distance calculation." Why not do the distance calculation inside of the callback? Or, if you do a distance calculation periodically, or in response to an event, just test if myPos is defined yet before you do the calculation (and show a message like "still getting location..." if it's not yet set).
    – apsillers
    Aug 28, 2012 at 13:20
  • @apsillers: I did that. Thanks a lot. Async programming takes a while to grasp.
    – Mina
    Aug 28, 2012 at 13:55

1 Answer 1

0
                    <script lang="JavaScript" type="text/javascript">
                        $.post(window.baseUrl + 'includes/tagmap.php', 
                              { name:"<?php echo $value->business_name; ?>",
                                x: "<?php echo $value->x; ?>", 
                                y: "<?php echo $value->y; ?>",
                                action:"populate"}, function(data){
                                    var locdata = $.parseJSON(data);        
                                    function ShowMyLocation() {
                                        FindMyLocation(function(myPos) {
                                            myPos = this;                                                                                                                                     addMarker(locdata.x,locdata.y,locdata.name);
                                        })
                                        FindMyLocation();
                                    }
                                    ShowMyLocation();                                  
                              });                                  
                    </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.