android java 如何制作像谷歌导航地图这样的浮动地图?

问题描述

enter image description here

嗨,我需要使用 android/java 在其他类似的应用程序上制作浮动地图

这是我的代码

我正在使用服务

public class mymapservice extends Service {
int LAYOUT_FLAG;

View mFloatingView;
WindowManager windowManager;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent,int flags,int startId) {

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
        LAYOUT_FLAG= WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    }else {
        LAYOUT_FLAG= WindowManager.LayoutParams.TYPE_PHONE;
    }



    Intent dialogIntent = new Intent(this,MapActivity.class);
    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(dialogIntent);


    mFloatingView = LayoutInflater.from(this).inflate(R.layout.map_activity,null);

    WindowManager.LayoutParams layoutParams =  new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,LAYOUT_FLAG,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);
    layoutParams.gravity= Gravity.BottOM|Gravity.CENTER;
    layoutParams.y=0;


    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.addView(mFloatingView,layoutParams);
    mFloatingView.setVisibility(View.VISIBLE);



    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    if(mFloatingView!=null){
        windowManager.removeView(mFloatingView);
    }
}

}

这是我如何从主要活动启动服务

Intent intent = new Intent(MainActivity.this,mymapservice.class);
startService(intent);

清单

<activity android:name=".MapActivity" android:theme="@style/Theme.AppCompat.Dialog"/>

我使用了一个非常认的地图视图

公共类 MapActivity 扩展 AppCompatActivity 实现 OnMapReadyCallback { 私有 MapView mMapView;

private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_activity);

    // *** IMPORTANT ***
    // MapView requires that the Bundle you pass contain _ONLY_ MapView SDK
    // objects or sub-Bundles.
    Bundle mapViewBundle = null;
    if (savedInstanceState != null) {
        mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
    }
    mMapView = (MapView) findViewById(R.id.map);
    mMapView.onCreate(mapViewBundle);

    mMapView.getMapAsync(this);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
    if (mapViewBundle == null) {
        mapViewBundle = new Bundle();
        outState.putBundle(MAPVIEW_BUNDLE_KEY,mapViewBundle);
    }

    mMapView.onSaveInstanceState(mapViewBundle);
}

@Override
protected void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
protected void onStart() {
    super.onStart();
    mMapView.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    mMapView.onStop();
}

@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions().position(new LatLng(40.7812199,-73.9687025)).title("Marker"));
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40.7812199,-73.9687025),17f));
}

@Override
protected void onPause() {
    mMapView.onPause();
    super.onPause();
}

@Override
protected void onDestroy() {
    mMapView.onDestroy();
    super.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

地图布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="30dp">
    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/map"/>
    </RelativeLayout>
</RelativeLayout>

这是我的结果

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)