Google Maps toScreenLocation() 方法问题 Android

Issue with Google Maps toScreenLocation() method Android

如标题所述,我在使用 Google 地图时遇到了 toScreenLocation() 问题。

我试图在 Google 地图上选择任意点,我正在广播到我的 Google 地图片段。在该片段中,我想获取此 LatLng 坐标对应的屏幕点。但是,大多数时候我会收到如下错误:

Caused by: java.lang.IllegalArgumentException: left == right
        at android.opengl.Matrix.frustumM(Matrix.java:327)
        at com.google.maps.api.android.lib6.gmm6.o.b.b.j(Unknown Source)
        at com.google.maps.api.android.lib6.gmm6.o.b.b.l(Unknown Source)
        at com.google.maps.api.android.lib6.gmm6.o.b.b.a(Unknown Source)
        at com.google.maps.api.android.lib6.gmm6.o.b.b.c(Unknown Source)
        at com.google.maps.api.android.lib6.gmm6.c.y.a(Unknown Source)
        at com.google.maps.api.android.lib6.c.az.a(Unknown Source)
        at com.google.android.gms.maps.internal.ca.onTransact(SourceFile:74)
        at android.os.Binder.transact(Binder.java:380)
        at com.google.android.gms.maps.internal.IProjectionDelegate$zza$zza.toScreenLocation(Unknown Source)
        at com.google.android.gms.maps.Projection.toScreenLocation(Unknown Source)
        at com.example.tracker.googlemaps.GoogleMapsTracker.convertCoordinates(GoogleMapsTracker.java:163)
        at com.example.tracker.googlemaps.GoogleMapsTracker.access0(GoogleMapsTracker.java:38)
        at com.example.tracker.googlemaps.GoogleMapsTracker.onReceive(GoogleMapsTracker.java:153)
        at     android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)

转换对应的代码为:

    private void convertCoordinates(float latitude, float longitude){
    location = new LatLng(latitude, longitude);
    if(location.latitude != (float) 0.0 || location.longitude != (float) 0.0){
        //Point map = mapView.getMap().getProjection().toScreenLocation(location);

        Point map = projection.toScreenLocation(location);
        ScreenAdjustments.setLati(map.y);
        ScreenAdjustments.setLongi(map.x);
    } else {
        Log.e(TAG, "BAD COORDINATES!!!");
    }
}

我已经验证我得到的是浮点值,并且它们是我硬编码的值。但是大多数时候是碰到Point map = projection.toScreenLocation(location);它会抛出关于平截头体的错误。

我需要帮助来弄清楚如何让它工作。

我先试了你的代码,没有出错。

根据 here,我厌倦了 toScreenLocation 并且它有效。请记住设置 OnGlobalLayoutListener 以确保地图视图并等待布局过程完成。

示例代码:

public class MainActivity extends FragmentActivity {

    GoogleMap mGoogleMap;
    private static LatLng MountainView = new LatLng(37.42, -122.08);
    private static LatLng MountainView2 = new LatLng(37.421, -122.081);
    View mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Initial Map
        try {

            if (mGoogleMap == null) {
                mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        mapView = getSupportFragmentManager().findFragmentById(R.id.map).getView();

        mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
        mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
        mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);

        //mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(MountainView, 15));

        Marker marker = mGoogleMap.addMarker(new MarkerOptions()
                .position(MountainView)
                .title("Have a nice day!"));

        convertCoordinates((float) 37.42, (float) -122.08);

    }

    private void convertCoordinates(float latitude, float longitude) {
        final LatLng location = new LatLng(latitude, longitude);
        if (location.latitude != (float) 0.0 || location.longitude != (float) 0.0) {
            if (mapView.getViewTreeObserver().isAlive()) {
                mapView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        // remove the listener
                        // ! before Jelly Bean:
                        mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        // ! for Jelly Bean and later:
                        //mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        // set map viewport
                        // CENTER is LatLng object with the center of the map
                        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 15));
                        // ! you can query Projection object here
                        Point markerScreenPosition = mGoogleMap.getProjection().toScreenLocation(location);
                        // ! example output in my test code: (356, 483)
                        Log.d("x!!!!", markerScreenPosition.x + "");
                        Log.d("y!!!!", markerScreenPosition.y + "");
                    }
                });

            } else {
                //Log.e(TAG, "BAD COORDINATES!!!");
            }
        }
    }
}

在我的 Nexus 6 上有效,输出为 x = 639, y = 1049