0

I'm working on a Geolocation tool for a mobile website and so far i got this:

The Geolocation verification from here:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} else {
    console.log('Geolocation not enabled in your browser.');
}

The toRad() function from Caspar's answer here:

if (typeof(Number.prototype.toRad) === "undefined") {
    Number.prototype.toRad = function() {
        return this * Math.PI / 180;
    }
}

and the successFunction() from the first link, Movable Type Scripts and some others modifications:

function successFunction(position) {

    var lat1 = position.coords.latitude;
    var lon1 = position.coords.longitude;

    function calcdist(nLoja,nLat,nLong) {
        this.loja = nLoja;
        this.lat = nLat;
        this.long = nLong;
    }

    aLocal = new Array(3)
    aLocal[0] = new calcdist("leblon",-22.982279,-43.217792);
    aLocal[1] = new calcdist("ipanema",-22.98376,-43.212138);
    aLocal[2] = new calcdist("barra",-22.999118,-43.357867);

    var i = 0;
    //for (var i = 0; i < 4; i++){

        lat2 = aLocal[i].lat;
        lon2 = aLocal[i].long;

        var R = 6371;
        var dLat = (lat2-lat1).toRad();
        var dLon = (lon2-lon1).toRad();
        var lat1 = lat1.toRad();
        var lat2 = lat2.toRad();

        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d = R * c;

        var thediv = document.getElementById('locationinfo');
            thediv.innerHTML = '<p>Distance user-' + aLocal[i].loja + ': ' + d + '</p>';

    //}
}

This works just fine on my desktop and my iphone. The problem comes when i comment the i=0; line and uncomment the for() statement right after (as well as the closing bracket). Chrome's console returns me an error:

Uncaught TypeError: Cannot read property 'lat' of undefined

For this line:

lat2 = aLocal[i].lat;

The reason for the loop is that i want to do the math x times to check which shop is closer to the user (by getting the smallest distance between user and each shop). But i can't seem to find out why it won't allow the loop.

Thanks a lot in advance.

2
  • not sure if it will fix the error, but you should be using var when you declare your variables. Commented Oct 31, 2012 at 19:40
  • thanks @sachleen for the little edits i had forgotten and the help. Commented Oct 31, 2012 at 19:54

2 Answers 2

2

Your loop condition is i<4 so i will be 0, 1, 2, 3, but you only have array indices 0, 1, 2

You can't go past the length of the array, so change the loop condition to go until the length of the array using i < aLocal.length

for(i = 0; i < aLocal.length; i++) {
Sign up to request clarification or add additional context in comments.

Comments

1

aLocal has three elements. Your loop goes from 0 to 3 so that are 4 elements.

for (var $i = 0; $i < aLocal.length; $i++) {
    //...
}

1 Comment

Damn, can't believe it was this simple. I guess i have been too long on top of it. Great. Now there's a distance bug from the second loop but i should be able to figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.