I am currently struggling with the following task:
- We have two pairs of latitude/longitude which determine a small line segment
- It is needed to get two pairs of latitude/longitude for a small line segment which is orthogonal to first one, intersects first line segment at one of it's ends, and distance between the points found must be equal to given amount of meters.
- It is guaranteed that each point will not be farther from another than 1-2 kilometers.
- Display these two lines on Google
What I've tried to do is: 1) Find the vector connecting two original points 2) Find orthogonal vector for it 3) Adjust the norm of the vector as such that it fulfills condition 2.
Now, the problem occurred on step 2. What I've tried first is basically this:
vecX=Lng2 - Lng1
vecY=Lat2 - Lat1
orthVecX=vecY
orthVecY=-vecX
This relation worked fine - rectangle was plotted as a, well, rectangle, but only if latitude was not much different from 0. At latitude 10 and larger it was obvious that two lines are not orthogonal. Initially I thought that's okay - I thought it was just projection distorting the line, but then I remembered Mercator projection is supposed to preserve angles the same. I thought that from condition about locality we can assume that longitude/latitude coordinate system can be treated as a regular Cartesian system, but looks like I am wrong. And now I'm kinda stuck - I honestly have no idea right now how could I do this task.
That happens because 1° in longitude corresponds to different lengths at different latitudes. A not 100% accurate but fast remedy could be as follows: divide the longitude of each point by the cosine of the corresponding latitude before computing the orthogonal vector, and perform the reverse transformation on the final points. This corresponds to using a Mercator projection.