Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:49:14+00:00 2026-06-01T23:49:14+00:00

I’m working on a mobile app with Phonegap which will use Geolocation. I used

  • 0

I’m working on a mobile app with Phonegap which will use Geolocation. I used Geolocation before but this time I considered that creating a new object wrapper for it would be better as a large part of the functionality is based on this and don’t want to end up with messy code.

The solution is probably straight forward but it beats me as I’ve never done anything very advanced in JS using objects. Here’s the code (removed unneeded parts) at the moment:

function Geolocation(maximumAge, accurate) {
    this.settings = {
            'maximumAge': maximumAge,
            'accurate': accurate
    }
}

Geolocation.prototype = {
    position: {
        latitude: null,
        longitude: null,
        altitude: null,
        accuracy: null,
        altitudeAccuracy: null,
        heading: null,
        speed: null
    },
    lastCheck: null,
    watchId: null,
    onSuccess: function(position) {
        this.position.latitude = position.coords.latitude;
        this.position.longitude = position.coords.longitude;
        this.position.altitude = position.coords.altitude;
        this.position.accuracy = position.coords.accuracy;
        this.position.altitudeAccuracy = position.coords.altitudeAccuracy;
        this.position.heading = position.coords.heading;
        this.position.speed = position.coords.speed;
        this.lastCheck = new Date(position.timestamp);
    }, 
    onError: function(error) {
        console.log(error.code+' '+error.message);
    },
    getCoordinates: function() {
        if (this.watchId == null) { 
            this.watchId = navigator.geolocation.watchPosition(this.onSuccess, this.onError, { maximumAge: this.settings.maximumAge, enableHighAccuracy: this.settings.accurate});
        }
        return {
            latitude: this.position.latitude,
            longitude: this.position.longitude
        };
    }
};

As you probably noticed already, when I call getCoordinates(), the callback success function is (I’m guessing) out of the scope of the object, therefore not knowing what “this” is… Any idea of how to get around this or what the correct implementation would be?

Thank you for your time!

Later edit: I know I need to modify the function to return the coordinates only after the position was found. Not sure on how to do this at the moment, but if you have any tips that would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:49:15+00:00Added an answer on June 1, 2026 at 11:49 pm

    OK, I figured it out, partly with help from this other thread regarding binding the scope for the callback function: JavaScript Callback Scope

    To return the coordinates I’m using a callback function. Included the code below and I’m marking this as community wiki – but if anyone has any suggestions please go ahead and make them. Everything seems to be working fine, but maybe I’m not doing something right here.

    function Geolocation(maximumAge, accurate) {
        this.settings = {
                'maximumAge': maximumAge,
                'accurate': accurate
        }
    }
    
    Geolocation.prototype = {
        position: {
            latitude: null,
            longitude: null,
            altitude: null,
            accuracy: null,
            altitudeAccuracy: null,
            heading: null,
            speed: null
        },
        lastCheck: null,
        callback: null,
        watchId: null,
        onSuccess: function(position) {
            this.position.latitude = position.coords.latitude;
            this.position.longitude = position.coords.longitude;
            this.position.altitude = position.coords.altitude;
            this.position.accuracy = position.coords.accuracy;
            this.position.altitudeAccuracy = position.coords.altitudeAccuracy;
            this.position.heading = position.coords.heading;
            this.position.speed = position.coords.speed;
            this.lastCheck = new Date(position.timestamp);
    
            var pos = {
                latitude: this.position.latitude,
                longitude: this.position.longitude
            };
                    // call callback with position and accuracy parameters
            this.callback(pos, this.position.accuracy);
        }, 
        onError: function(error) {
            console.log(error.code+' '+error.message);
        },
        getCoordinates: function(callback) {
                    // Helper function to bind scope to callback function as seen at https://stackoverflow.com/questions/183214/javascript-callback-scope
            function bind(scope, fn) {
                return function () {
                    fn.apply(scope, arguments);
                };
            }
                    // Assign the callback function to the local member
            this.callback = callback;
                    // watchPosition is a method that gets called each time the position changes. Making sure it's only getting called once (watchId can be used to stop the function when necessary
            if (this.watchId == null) {
                            // notice usage of the bind function here 
                this.watchId = navigator.geolocation.watchPosition(bind(this, this.onSuccess), bind(this, this.onError), { maximumAge: this.settings.maximumAge, enableHighAccuracy: this.settings.accurate});
            }
        }
    };
    

    Usage example:

    var geo = new Geolocation(3000, true);
    geo.getCoordinates(createMap);
    
    function createMap(position, accuracy) {
        // Your code here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.