Maartin
Maartin

Reputation: 91

Problem setting values in loop

I've got som problem setting some values in my js-code. I've tried to figure out what the problem is but still can't find the issue in the code.

It's the mostN, mostE, mostW, mostS values I want to set.

The alert at the end of the code i "Undefined". Why? And How do I fix it? Thx!

var mostN,
mostE,
mostW,
mostS;

for (i = 1; i < opts.markers.length; i += 1) //loops 5 times
{
$geocoder.geocode(
{
    address: opts.markers[i].address},
    function (result, status)
    {
        if (status === google.maps.GeocoderStatus.OK)
        {
            var str = [result[0].geometry.location]; // "(42.456465, -48.74116)"
            var realString = str.toString();
            var stop = realString.length - 1;
            var real = realString.substring(1,stop); // "42.456465, -48.74116"

            var arr = real.split(", ");

            var lat = arr[0].toString(); // "42.456465"
            var lng = arr[1].toString(); // "-48.74116"

            if(mostN > lat) { mostN = lat; }
            if(mostE < lng) { mostE = lng; }
            if(mostW > lng) { mostW = lng; }
            if(mostS < lat) { mostS = lat; }

            alert(mostN); // returns "Undefined"
        }
        else
        {
            if (opts.log) {console.log("Geocode was not successful for the following reason: " + status); }
        }
    }
);

}

Upvotes: 1

Views: 85

Answers (4)

RobG
RobG

Reputation: 147403

You don't seem to be taking into account the sign of the lat and long. For example,

> var mostN, 
> ...
> var lat = arr[0].toString(); // "42.456465"

From your post, lat is already a string so converting it to string seems pointless. Given that later an arithmetic comparison is performed, it would be better to convert it to number up front:

var lat = Number(arr[0]); // 42.456465

or

var lat = +(arr[0]); // 42.456465

.

>  ...
>     if(mostN > lat) { mostN = lat; }
>     if(mostE < lng) { mostE = lng; }
>     if(mostW > lng) { mostW = lng; }
>     if(mostS < lat) { mostS = lat; }

The variable mostN has not been initialised so in the comparison it is equivalent to zero, which is not greater than 42.456465, so the test returns false and mostN is not set. However, mostS will be set.

But the sign of the comparison is wrong - if the latitude is a bigger +ve number it is more northerly so:

    if (mostN < lat) { mostN = lat; }

is what you want, same for the rest of the tests. However, you also have to take into account the hemisphere where -10 is more northerly than -20, so (provided mostN and lat are numbers, not strings):

    if ((mostN + 90) < (lat + 90)) { mostN = lat; }

since latitude is never greater than 90 degrees. For longitude, use 180. You can use any number you like that is 90 or greater for lat and 180 or greater for long, so you could use 180 for both but that might be confusing for maintainers.

Upvotes: 1

sje397
sje397

Reputation: 41822

Try something like:

var SOMETHING_HUGE = 100000000;
var mostN = -SOMETHING_HUGE,
mostE = -SOMETHING_HUGE,
mostW = SOMETHING_HUGE,
mostS = SOMETHING_HUGE;

//...

if(lat > mostN) { mostN = lat; }
if(lng > mostE) { mostE = lng; }
if(lng < mostW) { mostW = lng; }
if(lat < mostS) { mostS = lat; }

Upvotes: 1

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 8856

Your mostN is not clearly initialize with some value.Try to place alert inside

if(mostN > lat) { mostN = lat; }

condition. And also check the if comparison is correct or not.

Upvotes: 0

pabdulin
pabdulin

Reputation: 35229

Works as it should, since at the moment of check variable mostN is undefined, and condition mostN > lat is false.

Upvotes: 0

Related Questions