Geolocation GPS Updates Too Frequent

Discussion in 'JavaScript' started by Liam O'Byrne, Jun 17, 2013.

  1. #1
    Instruction
    Navigate to the following link using your Android/Smart phone via GPS (not Wi-Fi).

    www.liamobyrne.com/CycleMile/media/pages/track.html

    Click 'Start' and wait for your position to be located. It should display a map of your position and begin to update frequently.

    Problem
    My problem is that the updates are too frequent and ALWAYS cause my mobile browser to crash.

    Question
    Is there anyway (using JavaScript) that I can control the update time?
     
    Liam O'Byrne, Jun 17, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I would think the problem isn't so much the refresh rate, as it is how the data on the display is BEING refreshed -- you've got a part in there that's building a dozen or so textnodes and data using innerHTML, and that would force a full page reflow and take far, FAR more time than if you just put those elements in the DOM, and then set their values.

    Setup: (should be run ONCE to put these on the DOM)

    var info = document.getElementById('info');
    
    info.makeDataRow = function(title,extraText) {
    	var newE = this.appendChild(document.createElement('div'));
    	newE.appendChild(document.createTextNode(title));
    	var result = newE.appendChild(document.createTextNode('-'));
    	if (extraText) newE.appendChild(document.createTextNode(extraText));
    	this.appendChild(newE);
    	return result;
    }
    
    info.fieldData = {
    	latitude : info.makeDataRow('Latitude:'),
    	longitude : info.makeDataRow('Longitude:'),
    	accuracy : info.makeDataRow('Altitude:'),
    	altitude : info.makeDataRow('Altitude Accuracy:'),
    	altitudeAccuracy : info.makeDataRow('Heading:'),
    	speed : info.makeDataRow('Speed:',' (mph)'),
    	timestamp : info.makeDataRow('Timestamp:'),
    	distance : info.makeDataRow('Distance Travelled:',' (miles)')
    };
    Code (markup):
    Then inside your update function only update the values.

    info.fieldData.latitude.nodeValue = position.coords.latitude;
    info.fieldData.longitude.nodeValue = position.coords.longitude;
    info.fieldData.accuracy.nodeValue = position.coords.accuracy;
    info.fieldData.altitude.nodeValue = position.coords.altitude;
    info.fieldData.altitudeAccuracy.nodeValue = position.coords.altitudeAccuracy;
    info.fieldData.heading.nodeValue = position.coords.heading;
    info.fieldData.speed.nodeValue = position.coords.speed * 2.2369;
    info.fieldData.timestamp.nodeValue = new Date(position.timestamp).toLocaleString();
    info.fieldData.distance.nodeValue = distance;
    Code (markup):
    That would run many, MANY times faster and possibly eliminate the crashing issue.

    It's one of the reasons we've been told to STOP using innerHTML for some... seven or eight YEARS. Updating just the parts that need changing on the DOM is going to be WAY faster than sending that massive innerHTML with string addition.

    It also seems like you are calling various google maps CREATE methods far, far more than you should -- It almost seems like you're creating them without DISCARDING them, and that too could be making mobile devices (or even non-mobile given enough time) go ker-plooey.
     
    deathshadow, Jun 18, 2013 IP
  3. Liam O'Byrne

    Liam O'Byrne Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Hi @deathshadow

    Thank you for your response.

    I understand your explanation above, and it could possibly solve the crash issue I seem to be encountering. However, I believe there may be an easier way to solve this problem.

    With the watchPosition() method using GPS, it updates both the map and positional values.

    Instead of updating the map, why don't I just update the map cursor instead?

    Could this be achieved using JavaScript?
     
    Liam O'Byrne, Jun 20, 2013 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    I'm not that familiar with the Google Maps API, but that sounds like it would probably be the best approach. The constantly creating a new map every time is some hefty overhead.
     
    deathshadow, Jun 20, 2013 IP