Using a Projection class
Google maps provides a Projection
class, which has some useful methods. The fromScreenLocation()
method converts screen pixels in point coordinates to LatLng
coordinates.
The toScreenLocation()
method converts the LatLng
coordinates to the screen pixel point coordinates.
The getVisibleRegion()
method returns the VisibleRegion
object from which we can obtain LatLngBounds
.
Let's see an example where we are going to convert a screen pixel Point coordinate, (100,100), to a LatLng
coordinate when the camera changes. From this, we get the visible region and display the north-east and south-west corners of the LatLng
coordinate.
Then, when the map is clicked on, we obtain the LatLng
coordinate of the clicked location and convert it to the screen pixels point coordinate.
The complete code for the onMapReady()
callback method is as follows:
@Override public void onMapReady(final GoogleMap map) { map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() ...