6

Tôi đã cố gắng giải quyết vấn đề này một thời gian. Tôi muốn sử dụng một MapView v2 với hình nền sống của tôi, cho đến nay kết quả của tôi là một màn hình màu đen với một biểu tượng google ở ​​phía dưới.Sử dụng MapView v2 của Google trong Hình nền động

quyền

results

manifest của tôi đã yêu cầu:

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

và meta-data:

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

Và tôi cũng đã cố gắng thay đổi tăng tốc phần cứng như đề xuất trong một số câu hỏi MapView liên quan khác:

android:hardwareAccelerated="false" 

Ngoài ra tôi có thể khẳng định rằng chìa khóa bản đồ Api là thực sự đúng, vì tôi có một MapFragment mà ám đúng cách (không có trong dịch vụ wallaper nhưng trong một hoạt động thử nghiệm): Mã

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 không hiển thị bất kỳ lỗi hay bất kỳ gợi ý về những gì có thể là vấn đề

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 

Tôi hiểu rằng WallpaperService không được thiết kế để sử dụng với các phần tử giao diện người dùng thông thường, tuy nhiên có thể sử dụng chúng và tôi đã thực hiện thành công với TextViews và như vậy. Nó có phải là một hạn chế được đặt trên MapView một cách có mục đích và tôi đang đánh bại một con ngựa chết? Cảm ơn bạn

Trả lời

-2

Nếu bạn đang cố gắng chụp ảnh bản đồ của mình, bạn có thể sử dụng phương pháp sau. Bằng cách xác định biến "đường dẫn", bạn có thể lưu ảnh và sử dụng lại ở nơi khác. Thay thế: bạn có thể lưu Bitmap nội bộ và sử dụng lại nó.

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); 
      } 
     } 
    }); 
} 

của nó rất quan trọng để gọi phương pháp này từ phương pháp này OnMapLoadedCallback thích:

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() { 
    @Override 
    public void onMapLoaded() {savePhoto();} 
}); 
Các vấn đề liên quan