在动态壁纸中使用谷歌的MapView v2

时间:2017-07-14 16:56:28

标签: android google-maps google-maps-android-api-2 android-mapview live-wallpaper

我一直试图解决这个问题。我想在我的动态壁纸中使用MapView v2,到目前为止我的结果是黑屏,底部带有谷歌徽标。

results

我的清单需要权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

和元数据:

 <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

 <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

我还尝试按照其他一些MapView相关问题的建议更改硬件加速:

android:hardwareAccelerated="false"

此外,我可以确认地图Api键确实是正确的,因为我有一个正确渲染的MapFragment(不是在wallaper服务中,而是在测试活动中):

MapFragment

WallpaperService.Engine代码:

private class ENGINE extends Engine implements OnMapReadyCallback, 
GoogleMap.SnapshotReadyCallback {

    private final String LOG_TAG =  ENGINE.class.getSimpleName();
    private int width = 0;
    private int height = 0;
    private SurfaceHolder holder;
    private boolean visible;
    private Bitmap bitmap;

    private final Handler handler = new Handler();
    private final Runnable drawRunner = new Runnable() {
        @Override
        public void run() {
            draw();
        }
    };

    MapView mapView;
    GoogleMap map;
    private static final int FPS = 5000;

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);
        this.holder = surfaceHolder;


        this.width = getDesiredMinimumWidth();
        this.height = getDesiredMinimumHeight();

        int margin = 100;
        Rect rect = new Rect();
        rect.set(margin, margin, width, height);


        mapView = new MapView(getApplicationContext());
        mapView.onCreate(new Bundle()); // could this be the problem?
        // since onCreate(null) has exact same result

        int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY);
        int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY);

        mapView.measure(widthSpec, heightSpec);
        mapView.layout(0, 0, rect.width(), rect.height());


        Log.d(LOG_TAG, "Width: " + mapView.getMeasuredWidth());
        Log.d(LOG_TAG, "Height: " + mapView.getMeasuredHeight());

        mapView.onResume();
        try {
            MapsInitializer.initialize(getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mapView.getMapAsync(this);
    }

    public ENGINE() {
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        this.visible = visible;
        if (visible) {
            handler.post(drawRunner);
            mapView.onResume();
        } else {
            handler.removeCallbacks(drawRunner);
            mapView.onPause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        handler.removeCallbacks(drawRunner);
    }

    private void draw() {
        final Canvas canvas = holder.lockCanvas();
        try {
            if (canvas != null) {
                if (mapView != null) {
                    mapView.draw(canvas);
                    Log.d(LOG_TAG, "Drawing Map view on the canvas");
                } else {
                    Paint paint = new Paint();
                    paint.setColor(getColor(R.color.colorAccent));
                    paint.setStyle(Paint.Style.FILL);
                    canvas.drawPaint(paint);
                }
            }
        } finally {
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        }

        handler.removeCallbacks(drawRunner);
        if (visible) {
            handler.postDelayed(drawRunner, FPS);
        }
    }

    @Override
    public void onSnapshotReady(Bitmap bitmap) {
        // never reaches here
        this.bitmap = bitmap;
        Log.d(LOG_TAG, "Bitmap ready");
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.map = googleMap;

        //this.map.setMyLocationEnabled(true);

        this.map.getUiSettings().setZoomControlsEnabled(true);
        this.map.getUiSettings().setMyLocationButtonEnabled(true);
        this.map.getUiSettings().setCompassEnabled(true);
        this.map.getUiSettings().setRotateGesturesEnabled(false);
        this.map.getUiSettings().setZoomGesturesEnabled(false);

        Log.d(LOG_TAG, "Map is ready");


        double latitude = 56.875300;
        double longitude = -76.783658;


        LatLng marker = new LatLng(latitude, longitude);
        this.map.addMarker(new MarkerOptions().position(marker).title("Marker Title"));


        this.map.snapshot(this); 
        // I tried using map's snapshot to render it, however this callback never returns a value
    }

}

Logcat没有显示任何错误,也没有任何关于可能是什么问题的提示

07-14 10:26:39.213 3167-3167/removed.for.privacy I/Google Maps 
Android API: Google Play services package version: 11055440

07-14 10:26:39.398 3167-3195/removed.for.privacy D/OpenGLRenderer: 
endAllActiveAnimators on 0x75f689e800 (RippleDrawable) with handle 
0x75f65bede0

07-14 10:27:02.772 3167-3167/removed.for.privacy I/Google Maps 
Android API: Google Play services package version: 11055440

07-14 10:27:02.838 3167-3167/removed.for.privacy 
D/ENGINE : Width: 980

07-14 10:27:02.838 3167-3167/removed.for.privacy
D/ENGINE : Height: 1820

07-14 10:27:02.867 3167-3167/removed.for.privacy 
D/ENGINE : Map is ready

07-14 10:27:02.885 3167-3195/removed.for.privacy D/OpenGLRenderer: 
endAllActiveAnimators on 0x75f689ec00 (RippleDrawable) with handle 
0x75e6820020

07-14 10:27:02.900 3167-3167/removed.for.privacy 
D/ENGINE : Drawing Map view on the canvas

07-14 10:27:07.955 3167-3167/removed.for.privacy 
D/ENGINE : Drawing Map view on the canvas

据我所知,WallpaperService并不是设计用于常规UI元素,但可以使用它们,我成功地使用TextViews等。它是否有目的地限制在MapView上并且我击败了一匹死马?谢谢

3 个答案:

答案 0 :(得分:0)

精简模式是我让地图变得可见的唯一方法。

mapView = new MapView(context, new GoogleMapOptions().liteMode(true));

但无法进行倾斜,变焦和旋转。

答案 1 :(得分:-1)

无论如何,您可以使用Google Static Maps API来获取壁纸图片(例如,在此请求中:https://maps.googleapis.com/maps/api/staticmap?center=0,0&zoom=1&scale=1&size=300x500&maptype=roadmap&format=png&visual_refresh=true),然后,只需在壁纸上显示(并通过计时器或动作重新加载和更新屏幕,如果你需要)。

答案 2 :(得分:-2)

如果您要拍摄地图照片,可以使用以下方法。通过定义变量&#34; path&#34;你可以保存图片并在其他地方重复使用。 替代方案:您可以在内部保存位图并重复使用它。

public void savePhoto(){
    mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
        @Override
        public void onSnapshotReady(Bitmap bitmap) {
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(new File(path), true);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            }
            catch (Exception e) {e.printStackTrace();}
            finally {
                try {if (out != null) {out.close();}}
                catch (IOException e) {e.printStackTrace();}
                saveForm(isArea, datum, path);
            }
        }
    });
}

从OnMapLoadedCallback方法调用此方法非常重要,如:

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {savePhoto();}
});