1
votes

Ok, so I am trying to write a code which returns back the values of all the 4 corner position of the map according to current view able area. For example, a user access Google maps through his device, he focuses on a particular area and hits a button and gets back the position (latitude and longitude) of all the four corners.

For this I have the center coordinate and now all I want span of the current viewable area and get the top left and bottom right corner coordinates. for this I used,

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; // returns 480
int height = size.y; // returns 800

Now I want to convert them into latitude and longitude values so that I can use simple math such as:

topLeftLat= centerLat + lat/2;
topLeftLon= centerLon - lng/2;
bottomRightLat= centerLat - lat/2;
bottomRightLon = CenterLon + lng/2;

This would give me the corner cordinates. lat and lng being the converted sceen resolution in latitude and longtiude values.

1

1 Answers

2
votes

This took 5 minutes search on google so I don't know why I'm giving you this ... learning to research is the biggest part of being a developer.

GeoPoint mapCenter = myMap.getMapCenter();

   float centerLatitude = (float)(mapCenter.getLatitudeE6())/1000000;
   float centerLongitude = (float)(mapCenter.getLongitudeE6())/1000000;

   float latitudeSpan = (float)(myMap.getLatitudeSpan())/1000000;
   float longitudeSpan = (float)(myMap.getLongitudeSpan()/1000000);

   GeoPoint mapTopLeft = myMap.getProjection().fromPixels(0, 0);
   float topLatitude = (float)(mapTopLeft.getLatitudeE6())/1000000;
   float leftLongitude = (float)(mapTopLeft.getLongitudeE6())/1000000;

   GeoPoint mapBottomRight
   = myMap.getProjection().fromPixels(myMap.getWidth(), myMap.getHeight());
   float bottomLatitude = (float)(mapBottomRight.getLatitudeE6())/1000000;
   float rightLongitude = (float)(mapBottomRight.getLongitudeE6())/1000000;

String info =
     "Center: " + centerLatitude + "/" + centerLongitude + "\n"
     + "Top Left: " + topLatitude + "/" + leftLongitude + "\n"
     + "Bottom Right: " + bottomLatitude + "/" + rightLongitude + "\n"
     + "latitudeSpan: " + latitudeSpan + "\n"
     + "longitudeSpan: " + longitudeSpan + "\n";

   Toast.makeText(AndroidMapActivity.this,
     info,
     Toast.LENGTH_LONG).show();
  }};

The full tutorial is available at Android-er: Get the the coordinates (Latitude and Longitude) currently displayed on MapView