在服务中使用MapView

时间:2016-07-31 03:36:14

标签: android android-service android-mapview android-maps

我尝试使用以下代码在服务产生的弹出窗口中设置 mapview

<?xml version="1.0" encoding="utf-8"?>
  <com.mypackage.MyView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/tools"
    android:id="@+id/overlay_map_container"
    android:orientation="vertical"
    android:padding="8dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.google.android.gms.maps.MapView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/overlay_map" />
    </FrameLayout>
  </com.mypackage.MyView>

自定义视图:

public class MyView extends LinearLayout implements OnMapReadyCallback {
  private MapView mMapView;
  private GoogleMap mMap;

  public MyView(Context context) {
    super(context);
  }

  public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  @Override
  public void onFinishInflate() {
    mMapView = (MapView) findViewById(R.id.overlay_map);
    mMapView.onCreate(null);
    mMapView.getMapAsync(this);
  }

  @Override
  public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng target = Utils.getLocationFromAddress(getContext(), "Central Park");
    MarkerOptions markerOptions = new MarkerOptions().position(target);
    markerOptions.title(Utils.getAddressFromLatLng(getContext(), target));
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker());
    mMap.addMarker(markerOptions);

    CameraPosition.Builder builder = new CameraPosition.Builder();
    builder.zoom(16);
    builder.target(target);
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
  }

  public void onHide() {
    mMapView.onPause();
  }

  public void onShow() {
    mMapView.onResume();
  }

  public void onDestroy() {
    mMapView.onDestroy();
  }
}

从服务中,我只使用简单的 ChatHead

public class OverlayController {
  private Service mService;
  private WindowManager mWindowManager;
  private View mChatHead;
  private PopupWindow mPopUp;
  private MyView mMyView;

  private WindowManager.LayoutParams mChatHeadParams;

  public static Point szWindow = new Point();

  private boolean mShowing = false;

  public OverlayController(Service s) {
    mService = s;

    setup();
  }

  private void setup() {
    mWindowManager = (WindowManager) mService.getSystemService(WINDOW_SERVICE);

    final LayoutInflater inflater = (LayoutInflater) mService.getSystemService(LAYOUT_INFLATER_SERVICE);

    mChatHead = inflater.inflate(R.layout.chathead, null);

    mWindowManager.getDefaultDisplay().getSize(szWindow);

    mChatHead.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
          switch (motionEvent.getAction()) {
            case ACTION_DOWN:
              initialX = mChatHeadParams.x;
              initialY = mChatHeadParams.y;
              initialTouchX = motionEvent.getRawX();
              initialTouchY = motionEvent.getRawY();
              return true;
            case ACTION_UP:
              if ((Math.abs(initialTouchX - motionEvent.getRawX()) < 5) &&
                 (Math.abs(initialTouchY - motionEvent.getRawY()) < 5)) {

                if (mMyView == null) {
                  mMyView = (MyView) inflater.inflate(R.layout.myview, null);
                  mMyView.setLayoutParams(new LinearLayout.LayoutParams(szWindow.x, szWindow.y));
                }

                if (mPopUp == null) {
                  mPopUp = new PopupWindow(mMyView, szWindow.x, szWindow.y);
                  mPopUp.update();
                }

                if (mPopUp.isShowing()) {
                  mChatHeadParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
                  mChatHeadParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
                  mWindowManager.updateViewLayout(mChatHead, mChatHeadParams);

                   mPopUp.dismiss();
                   mMyView.onHide();
                } else {
                  mChatHeadParams.width = szWindow.x;
                  mChatHeadParams.height = szWindow.y;
                  mChatHeadParams.x = 0;
                  mChatHeadParams.y = 0;
                  mWindowManager.updateViewLayout(mChatHead, mChatHeadParams);

                  mPopUp.showAsDropDown(mChatHead, 0, 0);
                  mMiView.onShow();
                }
              }
              return true;
            case ACTION_MOVE:
              mChatHeadParams.x = initialX + (int) (motionEvent.getRawX() - initialTouchX);
              mChatHeadParams.y = initialY + (int) (motionEvent.getRawY() - initialTouchY);
              mWindowManager.updateViewLayout(mChatHead, mChatHeadParams);
              return true;
          }
        return false;
      }
    });
  }

  public void toggleOverlay(boolean toggle) {
    if (mChatHead != null) {
      if (toggle && !mShowing) {
        mShowing = true;
        mChatHeadParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_PHONE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
        mChatHeadParams.gravity = Gravity.TOP | Gravity.LEFT;
        mChatHeadParams.x = 0;
        mChatHeadParams.y = 100;
        mWindowManager.addView(mChatHead, mChatHeadParams);
      } else {
        if (mShowing) {
          mShowing = false;
          mWindowManager.removeViewImmediate(mChatHead);
        }
      }
    }

相关代码在这里!

如果我将它添加到 MapView 之上,我可以看到一点 ImageView ,只是为了检查弹出窗口和容器是否完全显示,它们是。地图只是白色!

我已经在另一个活动上运行了另一个MapView实例,只是为了排除任何权限/清单缺失。

有人能指出我的错误吗?谢谢!

0 个答案:

没有答案