简体   繁体   中英

Android Google Maps Update Camera Before Snapshot Out of Order

I am trying to snapshot the Google Map image AFTER I set the map to the bounds of the Polyline. I have used separate code samples found on StackExchange to do both the moveToBounds() and Snapshot which work fine individually, but when run in sequence the Snapshot is of the map image BEFORE the map was updated. I assume I need to insert a OnCameraChangeListener but I cannot make it work. Do I need to somehow nest Callbacks? Please advise.

public void mapCapture() {
    moveToBounds(gpsTrackingPolyline);
    mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
        public void onSnapshotReady(Bitmap bitmap) {
            // Write image to disk
            try {
                File bmpFile = new File(getApplicationContext().getExternalFilesDir(null), DEFAULT_BMP_FILENAME);
                FileOutputStream out = new FileOutputStream(bmpFile);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

private void moveToBounds(Polyline p)
{
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    List<LatLng> arr = p.getPoints();
    for(int i = 0; i < arr.size();i++){
        builder.include(arr.get(i));
    }
    LatLngBounds bounds = builder.build();
    int padding = 40; // offset from edges of the map in pixels
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
    mMap.animateCamera(cu);
}

Don't know if you have found solution or if you still need it. But still here it goes. Yes you need a callback which will let you know when map has finished animation. Do something like this:

mMap.animateCamera(cu, new GoogleMap.CancelableCallback() {

                @Override
                public void onFinish() {
                   //your code related to snapshot
                   mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
                       public void onSnapshotReady(Bitmap bitmap) {
                       // Write image to disk
                       //rest of your code  
                       });
                }
                @Override
                public void onCancel() {
                    // TODO Auto-generated method stub
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM