gpt4 book ai didi

android - 在动态壁纸中使用 Google 的 MapView v2

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:11 24 4
gpt4 key购买 nike

我一直在努力解决这个问题。我想将 MapView v2 与我的动态壁纸一起使用,到目前为止我的结果是黑屏,底部有一个谷歌标志。

results

我的 list 具有所需的权限:

<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"

我还可以确认 map Api key 确实正确,因为我有一个正确呈现的 MapFragment(不是在 wallaper 服务中,而是在测试 Activity 中):

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 施加了限制,而我正在击败一匹死马?谢谢

最佳答案

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

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

但是倾斜、缩放和旋转是不可能的。

关于android - 在动态壁纸中使用 Google 的 MapView v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com