GoogleMaps中的多个快照

时间:2016-09-26 08:15:04

标签: android google-maps-android-api-2

如何在每个满载的GoogleMaps中制作多个快照? 用这个实验,但当然它无法正常工作:

button.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {               
        for (int i = 0; i < markers.size(); i++)
        {
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(markers.get(i).getLatitude(), markers.get(i).getLongitude()))
                    .zoom(15).build();
            mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback()
            {
                Bitmap bitmap;

                @Override
                public void onSnapshotReady(Bitmap snapshot)
                {
                    bitmap = snapshot;
                    try
                    {
                        String time = String.valueOf(System.currentTimeMillis());
                        FileOutputStream out = new FileOutputStream("/mnt/sdcard/Android/data/com.app.my/files/" + time + ".png"); //just for test
                        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                    }
                    catch (Exception e)
                    {
                        Log.e(LogTag.MY_FILTER_ERROR, "Screenshot error", e);
                    }
                }
            };

            mGoogleMap.snapshot(callback);
        }
    }
});

它会生成所需数量的屏幕截图,但地图没有时间移动和加载。

1 个答案:

答案 0 :(得分:0)

我做到了。如果有人关心:

public void uploadScreenshots()
{
    if (getRootActivity() != null)
    {
        iterator = 0;
        map =...
        sortedKeys =...

        //move to the first marker
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(map.get(sortedKeys.get(iterator)).getLatitude(), map.get(sortedKeys.get(iterator)).getLongitude()))
                .zoom(15).build();
        mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 1, new GoogleMap.CancelableCallback()
        {
            @Override
            public void onFinish()
            {
                iterator++;
                mGoogleMap.setOnMapLoadedCallback(new CallBackLoadMap());
            }

            @Override
            public void onCancel()
            {
            }
        });

        //than recursively go to other markers
        mGoogleMap.setOnMapLoadedCallback(new CallBackLoadMap());
    }
}

class CallBackLoadMap implements GoogleMap.OnMapLoadedCallback
{
    @Override
    public void onMapLoaded()
    {
        final GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback()
        {
            Bitmap bitmap;

            @Override
            public void onSnapshotReady(Bitmap snapshot)
            {
                bitmap = snapshot;
                try
                {
                    String time = String.valueOf(System.currentTimeMillis());
                    FileOutputStream out = new FileOutputStream("/mnt/sdcard/Android/data/com.app.my/files/" + time + ".png"); //just for test - do not hardcode in your project!
                    bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                    }
                    catch (Exception e)
                    {
                        Log.e(LogTag.MY_FILTER_ERROR, "Screenshot error", e);
                    }
                }
            };

        mGoogleMap.snapshot(callback);

        if (iterator == sortedKeys.size())
            return;

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(map.get(sortedKeys.get(iterator)).getLatitude(), map.get(sortedKeys.get(iterator)).getLongitude()))
                .zoom(15).build();

        mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 1, new GoogleMap.CancelableCallback()
        {
            @Override
            public void onFinish()
            {
                iterator++;
                mGoogleMap.setOnMapLoadedCallback(new CallBackLoadMap());
            }

            @Override
            public void onCancel()
            {
            }
        });
    }
}

按预期工作 - 迭代所有标记,并在地图完全加载到此处后对每个标记进行屏幕截图。