The functionality of Google Maps' WatchPosition feature seems to be malfunctioning

One of the functionalities I'm working on involves tracking the user's position. First, I use getCurrentLocation to retrieve the user's location coordinates and then place a marker on the map to indicate their location (the marker variable is set globally). After that, I call the watchPosition function to continuously track any changes in the user's position. This piece of code is specifically created for an Ionic app, but the same concept can be applied to any web application (you can disregard $cordovaGeolocation as it is similar to navigator.geolocation).

var map = null;
var myPosition = null;

$scope.initMap = function(){
    var posOptions = {
        enableHighAccuracy: false,
        timeout: 20000,         
    };

    $cordovaGeolocation.getCurrentPosition(posOptions).then(
        function (position) {
            var lat  = position.coords.latitude;
            var long = position.coords.longitude;
            var myLatlng = new google.maps.LatLng(lat, long);               
            $scope.me = myLatlng;

            var mapOptions = {
                center: myLatlng,
                zoom: 11,
                disableDefaultUI: true,
                mapTypeId: google.maps.MapTypeId.ROADMAP,

            };          

            map = new google.maps.Map(document.getElementById("map"), mapOptions); 

            myPosition = new google.maps.Marker({
                map: map,
                position: myLatlng,
                draggable:false,
                icon: "https://i.sstatic.net/orZ4x.png",
            });             
            var circle = new google.maps.Circle({radius: User.getUser().distance*1000, center: myLatlng}); 
            map.fitBounds(circle.getBounds());              
            $cordovaGeolocation.watchPosition(win);
        },
        function(err) {
            console.log(err);
        }
    );
}

The callback function for the watchPosition method can be seen below:

var win = function(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    var myLatlng = new google.maps.LatLng(lat, long);
    if(myPosition==null){
        myPosition = new google.maps.Marker({
            map: map,
            position: myLatlng,
            draggable:false,
            icon: "https://i.sstatic.net/orZ4x.png",
        });
        myPosition.setMap(map);
    }else{
        myPosition.setPosition(myLatlng);
    }
    map.panTo(myLatlng);
};

Answer №1

The problem I encountered was related to $cordovaGeolocation, but I managed to resolve it by using navigator.geolocation instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rely on the pre-installed package

Currently, I am creating a intelligent package that relies on 'showdown', a pre-installed Meteor package. How should I indicate this dependency? Even after adding packages: { 'showdown': {} } to smart.json, I encountered an error st ...

Obtain the outline shape in ThreeJS (duplicate border)

When working with a geometry, I often need to view it from different angles. For example, if I position my camera from the top, I want to be able to extract the outline of the geometry as a new shape. This allows me to then Extrude this shape further. In R ...

Tips for Implementing a "Please Hold On" Progress Bar in ASP.NET

I have a master page named siteMaster.master, an aspx page called submission.aspx, and a user control named attachment.ascx. The script manager is included in my master page. The submission page inherits the master page and registers the user control attac ...

Create a Vue component that utilizes the v-for directive to iterate through a list of items, passing data from a

Imagine having two arrays with a similar structure like this: const individuals = [{name: "Jane Doe", id: "0001"},{name: "John Doe", id:"0002"}, {name: "Sam Smith", id: "0003"}, {name: "Joe ...

Material-Table React with a Fixed Header

After examining this specific example, I have a query regarding a sticky header feature. Here is the example link: https://material-table.com/#/docs/features/fixed-columns I have been attempting to make the header from 'Name' to 'BirthPlace ...

UI-Grid is encountering an issue with strict mode where multiple definitions of a property are not allowed. This error specifically occurs in Internet Explorer

Encountering error messages while loading Ui-Grid in Internet Explorer - [true] [SYSERR] Multiple definitions of a property not allowed in strict mode [object Object] https://i.sstatic.net/mq3uP.png The issue is specific to IE, without any errors in Fi ...

JavaScript Function Not Executed in Bottom Section

I've implemented AngularJS includes in my project. Below is the code snippet from my index.html: <!DOCTYPE html> <html ng-app=""> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> ...

Exploring deep layers of nested arrays within a JSON structure

Calculate the sum of ages for dogs in a given dataset and convert them to dog years. mL/hr I am looking for the optimal method to access array elements within a JavaScript object. For example, I want to retrieve the first faculty name and specialization ...

Convert the button into text once the condition is met

I am currently utilizing the AppBar component from the material-ui library and have added a button. I would like to change this button to display just text within the AppBar, but I haven't been successful. Is there a way to transform the button in th ...

Encountered an Unpredictable SyntaxError: Is this a Cross-Domain Problem?

I have been attempting to connect with the Indian Railway API using Ajax in order to retrieve data in JSON format. Below is the code I am using: <!DOCTYPE> <html> <head> <meta charset="UTF-8"> <script src="https://ajax.googleap ...

What causes my ready function to consistently execute upon controller or directive reinitialization?

Every time my controller or directive reinisilize, the angular.element(document).ready(function(){...}); function is executed. Why does this happen? In my scenario, I have two controllers and one directive. I update a value in the parent controller which ...

Changing view upon button click in ReactJS: implement conditional rendering

After grasping the fundamentals of ReactJS, I am eager to put my knowledge into practice by building a small application. The application includes two images below. In the Home view (1st image), there are several buttons that, when clicked, should lead to ...

The canvas game's animation can only be activated one time

I am currently working on designing a straightforward canvas game: Here is the code snippet located on CodePen var canvas; var ctx; var x = 300; var y = 400; var r = 0; var mx = 0; var my = 0; var WIDTH = 600; var HEIGHT = 400; function circle(x,y,r) ...

Is there a way to compel the browser or JavaScript to delete/disregard a cached file?

I've encountered an issue with my Javascript program that extracts data from a text file and displays it in a table. The problem arises when I update the text file - the changes don't reflect on the table because the browser is caching the file. ...

Configure your restify REST API server to handle both HTTPS and HTTP protocols

I'm currently utilizing node.js restify version 4.0.3 The code snippet below functions as a basic REST API server supporting HTTP requests. An example of an API call is var restify = require('restify'); var server = restify.createServer( ...

Managing data from two tables in Node.js with ejs

I have a question regarding my node.js project that I need help with. As a beginner in this field, I believe the answer may be simpler than anticipated. In my code file named index.js, I found the following snippet after referring to some online documenta ...

Guide on how to deactivate a button in Ionic 2 when the form fields are either empty or do not pass

I am a beginner with Ionic 2 and I am currently working on creating a new form. I want to add validation to the form fields, so I have applied validation in home.ts. export class HomePage { user: HomeUser[]; constructor(public fb: FormBuilder,pr ...

Creating custom markers with an API in Vue-2-Leaflet

I'm having trouble displaying markers using an API. I can retrieve the data from the API and store it in a variable, but for some reason the markers aren't showing up when I try to display them using a v-for loop. Any assistance would be greatly ...

Convert the dynamic table and save it as a JSON file

Looking for the most effective method to save dynamic table data into JSON format. I have two tables that need to be saved into a single JSON file. While I can easily access and console log the regular table data, I'm having trouble retrieving the td ...

Vue.js: The Power of Manipulating Strings

Why is the filter method not working with this.userId, but it is working with the hard-coded value "admin"? How can I resolve this issue? computed: { UidMessages: function() { return this.Messages.filter(function(m) { return m ...