[JAVA] Note that the Google Maps Android API GoogleMap.getProjection is not a singleton

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

Note that the Google Maps Android API GoogleMap.getProjection is not a singleton
Create a tweet heatmap with the Google Maps API
About the camera change event of Google Maps Android API
[Google Maps API] Map is not displayed and becomes blank [Rails]
A class that hits the DMM API
Find a building on Google Earth that is the same height as Shingodzilla
There is a pattern that the program did not stop when using Python threading
Thorough efficiency improvement! The name is not the only "short circuit evaluation" that has a cool name.
A story that pyenv is stuck because the python execution command PATH does not pass
A note about hitting the Facebook API with the Python SDK
Creating a Python script that supports the e-Stat API (ver.2)
Is there a bias in the numbers that appear in the Fibonacci numbers?
The story of creating a database using the Google Analytics API
[Map display] Display a map from the address registered by the user using the Google Maps JavaScript API and Geocoding API!
Python list is not a list
The image is a slug
When incrementing the value of a key that does not exist
Play a sound in Python assuming that the keyboard is a piano keyboard
The story of the release work of the application that Google does not tell
[python] A note that started to understand the behavior of matplotlib.pyplot
The NVM Checksum Is Not Valid, a solution to the problem that Intel's wired LAN is not recognized on Linux
The problem that give me chocolate is not made even if the correspondence analysis is done with COTOHA API