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

  • SEARCH
  • Home
  • 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 6473959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:31:43+00:00 2026-05-25T06:31:43+00:00

I wanted to play with geolocation API on my Android. I know that there

  • 0

I wanted to play with geolocation API on my Android. I know that there is a “navigator” object that is defined and that should be used to aquire user position. So, I created this sample code:

function GeolocationTester()
{
    // here I want to store all acquired locations
    this.locations = new Array();
    alert("this.locations defined: " + this.locations);
    this.onSuccess = function(position)
    {
        alert("Entered onSuccess");
        alert("this.locations defined: " + this.locations);
    }

    this.onError = function(error)
    {
        alert("error acquiring location");
    }
    navigator.geolocation.watchPosition(this.onSuccess, this.onError, { enableHighAccuracy: true });
}

And it doesn’t work for me. Each time watchPosition call onSuccess the this.locations field isn’t defined (and it is defined just after new Array). I known that I’m doing somethind wrong, but as it is one of my JavaScript attempts, not sure what. So, anybody could find a problem here?

  • 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-05-25T06:31:43+00:00Added an answer on May 25, 2026 at 6:31 am

    The problem is with the scoping of this. When the onSuccess or onError is called, this isn’t bound to the object containing the locations array. You need to create an explicit variable outside of the functions to which the array should be assigned and then use this variable in the callbacks, like this:

    var allLocations = this.locations = [a, b, c];
    this.onSuccess = function(position) {
        alert("allLocations: " + allLocations);
        alert("this.locations: " + this.locations);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wanted to know if someone had a suggestion on code or maybe there's a
I never used ruby before, I just wanted to play around with HAML and
I wanted to create a layout like the CyanogenMod call log in that there
This might sound like a vague question, but i wanted to know if there
I am building a small OS. For that i wanted to play audio. I
I wanted to code a resizeable WindowsMediaplayer (ActiveX) without the play controls. it should
I wanted to play around with some 3D controls in WPF, but was mildly
I'm just starting to play around with GTK+ and I wanted to stop bad
I wanted some of those spiffy rounded corners for a web project that I'm
I just wanted to play around with the jdk . Like changing the implementation

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.