ThinkGeo.com    |     Blog    |     Wiki    |     Support

Wrong longitude for mouse location across dateline

I noticed when using the wrapdateline mode the X values go past -180 and 180. To fix this I used:

if (x < -180 || x > 180)
{
    x %= 180;
}

However, this doesn’t really work because once you go past 180 it should hit -179, instead it hits 181 which would give 1 using the formula above. I feel like this is something that should be figured out automatically when using the wrap mode.

Any ideas?

Hi Dan,

Do you think this function works for you?

    private int convert(int value)
    {
        if (value < -180 || value > 180)
        {
            value += 180;

            if (value < 0)
            {
                value %= 360;
                value += 180;
            }
            else if (value > 360)
            {
                value %= 360;
                value -= 180;
            }
        }

        return value;
    }

Regards,

Ethan

Thanks Ethan.

Was able to shorten it down a bit by using the following:

((x + 180) % 360) - 180

also works for Latitude if anyone needs it

((y + 90) % 180) - 90

Note: Because modulus doesn’t work properly with doubles/floats all the time, you’ll want to use this or something similar:

double xRemainder = (worldPoint.X + 180) - (Math.Floor((worldPoint.X + 180) / 360) * 360);
double x = xRemainder - 180;

Hi Dan,

Thanks for your share.

Any question please let us know.

Regards,

Ethan