1

Suppose if a marker's longitude is 124.4567. How can I calculate the longitude which is 1 centimetre away to the left side of the screen? It will vary depending on the screen density and zoom level. Is there any inbuilt method to calculate that longitude?


PS: I am sorry, it was "longitude". I always am confused by the two. I have edited the question.

I am not trying to calculate geological distances between two markers. Basically, I want to know how much longitude is 1 centimetre on the screen (not 1 centimetre of actual land) of the device. I mean, 1cm on the screen could be 30 degree longitude difference if I have zoom it out on my phone, but 1cm on the screen could be 1 degree longitude on your phone if you have zoomed it in.

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

3 Answers3

0

You got distance calculation between 2 coordinator from here:

Distance betwee n coordinator

And you have destination long, source long, source lat, and distance just find out destination lat.

And about zoom level, you have scale ratio of google map here:

Google map scale ratio

0

"To the left side" means western direction. Actually, latitude varies from -90 to 90 degrees, so there is no latitude 124.

If you have any point LatLng, you can find point 1 meter to the left via simple math.

Radius of a parallel ring is r = R * cos(latitude_in_radians), so 1 meter takes 360 / r of longitude.

Therefore you can calculate your point as follows without any library.

var EarthR = 6378137;
var point = {lat: 45, lng: 124};
var point2 = {lat: point.lat, lng: point.lng - 360 / ( EarthR * Math.cos(point.lat * Math.PI / 180))};
console.log(point2);
shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
0

After a lot of Google search, I have found this How to access Google Maps API v3 marker's DIV and its pixel position?. The answer showed how to convert screen locations to lat/lng coordinates. So I modified it a little to get the position 1 centimetre on the left, no matter what the zoom level is.

I put a marker on the left for visual debugging, when a marker is clicked. Here is the code. I am hoping it could be helpful to future people.

var dpi = resources.displayMetrics.densityDpi;
var pixelsInCm = (dpi/2.54).toInt();

var existingPoint = mMap!!.projection.toScreenLocation(marker.position);
var leftPoint = Point(existingPoint.x - pixelsInCm, existingPoint.y);
var leftLatLng = mMap!!.projection.fromScreenLocation(leftPoint);

//The code below is only for visual test. Not necessary.
var leftMarker = MarkerOptions()
leftMarker.position(leftLatLng);
leftMarker.title("1cm to the left");
mMap!!.addMarker(leftMarker);
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135