3

I'm trying to create a method which calculates the x and y in a grid and eventually the distance between that point and the middle. The problem is, I only know a few values. To explain the case a bit better an image: enter image description here (the values between '(..,..)' are lat/long combinations).

As you can see, I know the following values:

start of canvas: xy(0,0)
middle of canvas: xy(540,800) and lat/long(52.3702160, 4.8951680)
max dimension of canvas: x 1080, y 1600
point: xy(?,?) and lat/long(52.4167267, 4.8052174)
point: xy(?,?) and lat/long(52,2422306, 5.1129068)

First, I need to do something to calculate the missing x and y's from the points. I already tried doing the following:

        double mapWidth    = screenWidth;
        double mapHeight   = screenHeight;

// get x value
        x = (location.getLongitude()+180)*(mapWidth/360);

// convert from degrees to radians
        double latRad = location.getLatitude()*Math.PI/180;

// get y value
        double mercN = Math.log(Math.tan((Math.PI/4)+(latRad/2)));
        y     = (mapHeight/2)-(mapWidth*mercN/(2*Math.PI));

        point = new PointF((float)x,(float)y);

This works but I'm getting the wrong x and y values.

For example if my points lat/long are further away the x and y's are getting bigger (more to the middle). But they need to be more at the side because the lat/long point is further away.

All lat/long points inside 2km diameter need to be in my grid, if the point is for example 0.9km away from the center it needs to be nearly at the side.

After that I need to calculate the distance between the two points. I already got that part using the following:

Math.sqrt((point.x - point2.x) * (point.x - point2.x) + (point.y - point2.y) * (point.y - point2.y));

My main problem is calculating the x and y from my lat/long points.

If anyone wants to help, thanks in advance!

Marc
  • 1,094
  • 3
  • 17
  • 38

2 Answers2

0

I completely rethought my way of calculating the x and y. I solved it by doing the following:

double distanceMeters = mCurrentLocation.distanceTo(location);
        x = ((1000+distanceMeters)*mMiddleCoords.x)/1000; //1000 = radius.
        y = ((1000+distanceMeters)*mMiddleCoords.y)/1000;
Marc
  • 1,094
  • 3
  • 17
  • 38
  • Why is the radius 1000? – Rajesh J Advani Sep 22 '14 at 13:42
  • As I said in the question, the diameter is 2km. 1km is the radius so 1000m is the radius in meters. – Marc Sep 22 '14 at 13:54
  • Your question says you want to calculate distance in pixels, whereas you detail out kilometers. What is it that you are trying to calculate? Can you please clarify your question? – Rajesh J Advani Sep 23 '14 at 12:36
  • X and Y are in pixels. As I said in the question I already knew how to calculate the distance between points. In the example above I calculated the X and Y for the second point and then calculate the distance between the middle point (which I had already) and the new point (x,y). – Marc Sep 23 '14 at 12:42
  • Do remember that the distance between longitudes 0 and 1 on the equator (latitude 0) is 111km, while it is ZERO at the north pole (latitude 90). So your formula is incorrect. – Rajesh J Advani Sep 23 '14 at 12:51
  • That's why I'm using distanceTo. That method calculates the distance for me. I didn't write the distanceTo myself, it is part of the Location object. – Marc Sep 23 '14 at 13:56
0

You can't directly use the distance formula from latitude and longitude. You'll have to take into account the curvature of the sphere to calculate the distance.

The minimum distance between two points on a sphere (and hence earth, simplifying it to a perfect sphere) is the length of the chord on what is called the Great Circle running through those points. A Great Circle is a circle with its center running through the center of the sphere).

From Wikipedia:

C = SQRT(X^2 + Y^2 + Z^2)

where:

X = cos(lat2) * cos(long2) - cos(lat1) * cos(long1)
Y = cos(lat2) * sin(long2) - cos(lat1) * sin(long1)
Z = sin(lat2) - sin(lat1)

And the distance is (2 * R * arcsin(C/2)) where R is the radius of the earth or 6371 km

The other alternative - if you know you will always have the Android libraries - is the Location.distanceTo() method.

Rajesh J Advani
  • 5,585
  • 2
  • 23
  • 35