This is the story of the Google Maps Android API used when using Google Maps on Android.
One of the functions of this SDK is "mutual conversion between map coordinates (latitude and longitude) and screen coordinates". With this,
So, to do this, call fromScreenLocation
or toScreenLocation
with an instance of the Projection
class in GoogleMap.getProjection ()
.
Doesn't it feel like the instance you can get with this GoogleMap.getProjection ()
, a singleton, or the same instance whenever you get it? As with GoogleMap.getUiSettings ()
. Writing map.getProjection (). FromScreenLocation
is dull, so do you want to get it only once when generating a map and reuse it?
private Projection _proj;
@Override
public void onMapReady (GoogleMap map) {
//There was a time when I thought that I should get it first and reuse it ...
_proj = map.getProjection();
}
However, this is a mistake.
As a trial, write "If the position of the map changes, get the latitude and longitude of the upper left (0, 0) of the screen".
private Projection _proj;
@Override
public void onMapReady (GoogleMap map) {
//There was a time when I thought that I should get it first and reuse it ...
_proj = map.getProjection();
//Event when the camera is completely moved and idle (using RetroLambda)
map.setOnCameraIdleListener(() -> {
//Get the latitude and longitude of the screen origin
LatLng latlng = _proj.fromScreenLocation(new Point(0, 0));
Log.d("TEST", "lat:" + latlng.latitude + ", long:" + latlng.longitude);
});
}
When I scroll the map, the handler for setOnCameraIdleListener
is called, but I don't get the expected result. I think it's probably minus or close to zero.
To get the correct result
map.setOnCameraIdleListener(() -> {
//Get the latitude and longitude of the screen origin
Projection prj = map.getProjection();
LatLng latlng = prj.fromScreenLocation(new Point(0, 0));
Log.d("TEST", "lat:" + latlng.latitude + ", long:" + latlng.longitude);
});
You need to get a "current" Projection
instance, like.
Now let's take a look at the API Reference description.
The Projection returned is a snapshot of the current projection, and will not automatically update when the camera moves. As this operation is expensive, you should get the projection only once per screen. Google Maps uses the Mercator projection to create its maps from geographic data and convert points on the map into geographic coordinates.
The Projection returned is a snapshot of the current projection, and will not automatically update when the camera moves.
It was written firmly!
Projection is a projection method (Mercator projection), and since the projection method (= projection formula) is immutable, I didn't think that a snapshot would be created each time it was taken. I didn't notice this behavior and kept worrying about it for about 3 hours.
The lesson was that if you were worried, let's throw away the stereotypes and review Kihon. But let me just say that a method name like getCurrentProjection ()
might not have been the idea of "getting it first and reusing it". As an SDK creator, I wanted to consider the naming.
Recommended Posts