使用MapView的正确方法(在Android服务中)

时间:2017-09-02 01:35:13

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

我想要实现的目标:在背景服务或AsyncTask中获取位置的快照,然后我可以在这样的屏幕中向用户显示它们:

enter image description here

  1. 我在Android服务中
  2. 我想创建一个地图,添加一个标记然后拍摄它的快照(我将它保存在apps数据库中)
  3. 以下是我创建地图的方式:
  4. 我的服务:

    public class GetCurrentLocationService extends Service implements OnMapReadyCallback 
    (...)   
    
    
    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Util.log(TAG, "Service started");
    
            locationService = new LocationService(GetCurrentLocationService.this);
            mapService = new MapService(GetCurrentLocationService.this);
            mapService.setLiteMode(true);
            mapService.setAddMarker(true);
    
            mapService.connect();
    
            GoogleMapOptions options = new GoogleMapOptions().liteMode(true);
            mapView = new MapView(GetCurrentLocationService.this, options);
            mapView.onCreate(null);
            mapView.setLayoutParams(new MapView.LayoutParams(300, 400));
            Util.log(TAG, mapView.getHeight() + " " + mapView.getWidth());
            mapView.getMapAsync(this);
    
            return super.onStartCommand(intent, flags, startId);
        }
    
    1. 仍然在服务内部,当地图准备就绪时......

      @覆盖 public void onMapReady(GoogleMap googleMap){     Util.log(TAG,“Map ready:”+ googleMap);

      googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
      mapService.setMap(googleMap);
      
      try {
          location = mapService.getLastKnowLocation();
      
          if (location != null) {
              Util.log(TAG, "Got location: " + location.toString());
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      
      Util.log(TAG, "Get current location service done");
      
      stopSelf();
      

      }

    2. mapService.getLastKnowLocation()会像这样拍摄快照:

      public void takeSnapshot(boolean _saveIfPersisted){         this.saveIfPersisted = _saveIfPersisted;

      if(map != null) {
          Util.log(TAG, "Map ready: " + map);
      
          if(!liteMode) {
              map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                  public void onMapLoaded() {
                      snapshot();
                  }
              });
          } else {
              snapshot();
          }
      }
      

      }

      private void snapshot(){     map.snapshot(new GoogleMap.SnapshotReadyCallback(){         public void onSnapshotReady(Bitmap bitmap){             Util.log(标签,“快照准备就绪”);

              if (location != null) {
                  Util.log(TAG, "Location is ready");
      
                  // Resize the snapshot to thumbnail size
                  Bitmap resizedBitmap = scaleCenterCrop(bitmap, MAP_SNAPSHOT_WIDTH, MAP_SNAPSHOT_HEIGHT);
                  //bitmap.recycle();
      
                  // Set snapshot to the location and save it (if asked for)
                  location.setSnapshot(resizedBitmap);
      
                  LocationService locationService = new LocationService(context);
                  if (saveIfPersisted && locationService.isPersisted(location)) {
                      Util.log(TAG, "Location is persisted");
      
                      locationService.save(location);
                  }
              }
          }
      });
      

      }

    3. Heres LogCat:

      09-01 22:27:42.017 16520-16520 / br.org.knob.followme D / GetCurrentLocationSvc:服务已启动 09-01 22:27:42.022 16520-16520 / br.org.knob.followme I / Google Maps Android API:Google Play服务包版本:11509438 09-01 22:27:42.122 16520-16520 / br.org.knob.followme D / GetCurrentLocationSvc:0 0 09-01 22:27:42.123 16520-16520 / br.org.knob.followme D / GetCurrentLocationSvc:地图就绪:com.google.android.gms.maps.GoogleMap@2c0fed8 09-01 22:27:42.132 16520-16520 / br.org.knob.followme D / MapFragment:最后知道位置:位置[fused -30,051789,-51,197934 acc = 22 et = + 8d5h25m32s546ms] 09-01 22:27:42.183 3658-3668 /?我/艺术:背景部分并发标记扫描GC释放212329(10MB)AllocSpace对象,13(448KB)LOS对象,40%免费,19MB / 32MB,暂停1.325ms总计162.150ms 09-01 22:27:42.204 16520-16520 / br.org.knob.followme D / MapFragment:Marker添加 09-01 22:27:42.204 16520-16520 / br.org.knob.followme D / MapFragment:得到位置,现在尝试获取快照 09-01 22:27:42.205 16520-16520 / br.org.knob.followme D / MapSnapshotService:地图就绪:com.google.android.gms.maps.GoogleMap@2c0fed8 09-01 22:27:42.206 16520-16520 / br.org.knob.followme D / GetCurrentLocationSvc:获取位置:位置{id = null,latitude =' - 30.0517891',经度=' - 51.1979342'} 09-01 22:27:42.206 16520-16520 / br.org.knob.followme D / GetCurrentLocationSvc:获取当前位置服务 09-01 22:27:42.207 16520-16520 / br.org.knob.followme D / GetCurrentLocationSvc:服务被破坏 09-01 22:27:42.224 16520-17511 / br.org.knob.followme E / AndroidRuntime:致命异常:androidmapsapi-Snapshot                                                                       流程:br.org.knob.followme,PID:16520                                                                        java.lang.IllegalArgumentException:width和height必须是> 0                                                                           在android.graphics.Bitmap.createBitmap(Bitmap.java:903)                                                                           在android.graphics.Bitmap.createBitmap(Bitmap.java:882)                                                                           在android.graphics.Bitmap.createBitmap(Bitmap.java:849)                                                                           在com.google.maps.api.android.lib6.impl.eb.b(:com.google.android.gms.DynamiteModulesB@11509438:4)                                                                           在com.google.maps.api.android.lib6.impl.bf.run(:com.google.android.gms.DynamiteModulesB@11509438:4)

0 个答案:

没有答案