管灌系统巡查员智能手机App
zuoxiao
2024-11-13 e7df063a027c0f066317da4437d01cf3f3bc8d31
app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
@@ -3,30 +3,56 @@
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.ValueCallback;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import com.dayu.pipirrapp.bean.db.CenterPointBean;
import com.dayu.pipirrapp.bean.db.LatLonBean;
import com.dayu.pipirrapp.bean.db.MarkerBean;
import com.dayu.pipirrapp.bean.net.CenterPointResult;
import com.dayu.pipirrapp.bean.net.MarkerResult;
import com.dayu.pipirrapp.dao.DaoSingleton;
import com.dayu.pipirrapp.databinding.FragmentMapBinding;
import com.dayu.pipirrapp.js.MyWebViewInterface;
import com.dayu.pipirrapp.net.ApiManager;
import com.dayu.pipirrapp.net.BaseResponse;
import com.dayu.pipirrapp.net.Constants;
import com.dayu.pipirrapp.net.subscribers.SubscriberListener;
import com.dayu.pipirrapp.observer.MapFragmenObserver;
import com.dayu.pipirrapp.service.MyLocationService;
import com.dayu.pipirrapp.utils.CommonData;
import com.dayu.pipirrapp.utils.CommonKeyName;
import com.dayu.pipirrapp.utils.MapJpgUtils;
import com.dayu.pipirrapp.utils.MyLog;
import com.dayu.pipirrapp.utils.SharedPreferencesHelper;
import com.dayu.pipirrapp.utils.ToastUtil;
import com.dayu.pipirrapp.utils.WebViewUtils;
import com.dayu.pipirrapp.view.ConfirmDialog;
import com.jeremyliao.liveeventbus.LiveEventBus;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;
/**
 * author: zuo
@@ -35,128 +61,342 @@
 * 备注:地图页
 */
public class MapFragment extends BaseFragment {
    static String TAG = "MapFragment";
    FragmentMapBinding binding;
    //定位相关
    LocationManager locationManager;
    WebView mWebView;
    CenterPointBean centerPointBean;
    String strMarkerJson;
    boolean isStartInspec = false;
    MapFragmenObserver mapFragmenObserver;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        requireActivity().getLifecycle().addObserver(new MapFragmenObserver());
        Log.i(TAG, "onAttach");
        mapFragmenObserver = new MapFragmenObserver();
        requireActivity().getLifecycle().addObserver(mapFragmenObserver);
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");
        isStartInspec = SharedPreferencesHelper.getInstance(this.getContext()).get(CommonKeyName.isStartInspec, false);
    }
    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }
    /**
     * 修改巡检状态
     */
    private void chageInspecState() {
        Intent location = new Intent(this.getActivity(), MyLocationService.class);
        location.putExtra("isSingle", false);
        if (isStartInspec) {
            //获取定位服务传过来的坐标点
            LiveEventBus.get(CommonKeyName.locationData).observeForever(new Observer<Object>() {
                @Override
                public void onChanged(Object o) {
                    LatLonBean latLonBean = (LatLonBean) o;
                    mWebView.evaluateJavascript("javascript:updateLocation(\"" +  latLonBean.getLatitude()+ "\",\"" +  latLonBean.getLongitude()+ "\")", value -> {
                    });
                }
            });
            binding.stateText.setVisibility(View.VISIBLE);
            binding.inspectButton.setText("终");
            //开启定位
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                this.getActivity().startForegroundService(location);
            } else {
                this.getActivity().startService(location);
            }
        } else {
            binding.stateText.setVisibility(View.GONE);
            binding.inspectButton.setText("巡");
            //关闭定位
            this.getActivity().stopService(location);
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentMapBinding.inflate(inflater, container, false);
        mapFragmenObserver.setmWebView(binding.webView);
        Log.i("MapFragment", "onCreateView");
        mWebView = binding.webView;
        initWebView();
        mWebView.loadUrl("http://192.168.3.178:3000/");
        mWebView = WebViewUtils.initWebView(mWebView);
        MyWebViewInterface myWebViewInterface = new MyWebViewInterface(MapFragment.this);
        mWebView.addJavascriptInterface(myWebViewInterface, "Android");
        mWebView.loadUrl("file:///android_asset/index.html");
        getCenterPoint();
        initView();
        //开启定位
        Intent location = new Intent(this.getActivity(), MyLocationService.class);
        location.putExtra("isSingle", false);
        this.getActivity().startService(location);
        initLocalData();
        getMarkerData();
        //显示巡检状态
        chageInspecState();
        return binding.getRoot();
    }
    @Override
    public void onResume() {
        super.onResume();
        mWebView.onResume(); // 恢复 WebView,能正常执行网页的响应
    /**
     * 初始化各个状态
     */
    private void initState() {
        //开启定位
    }
    @Override
    public void onPause() {
        super.onPause();
        mWebView.onPause(); // 通过 onPause 动作通知内核暂停所有的动作,如 DOM 的解析、plugin 的执行、JavaScript 执行等
    /**
     * 初始化本地数据
     */
    public void initLocalData() {
        centerPointBean = DaoSingleton.getInstance(MapFragment.this.getContext()).centerPointDao().findFirst();
        List<MarkerBean> markerBeans = DaoSingleton.getInstance(MapFragment.this.getContext()).markerDao().findAll();
        strMarkerJson = WebViewUtils.beanToJson(markerBeans);
        jumpCenterPoint();
        setMapMarker();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ((ViewGroup) mWebView.getParent()).removeView(mWebView);
        mWebView.destroy(); // 当 Activity 要 destroy 时,应先将 WebView 移除,再 destroy 掉
    }
    void initWebView() {
//        getActivity().getWindow().setFlags(
//                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
//                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        // WebView 配置
        WebSettings webSettings = mWebView.getSettings();
        // 生命周期
// 前进后退
        if (mWebView.canGoBack()) {
            mWebView.goBack();
    /**
     * 跳转地图中心点
     */
    public void jumpCenterPoint() {
        if (centerPointBean != null) {
            Log.d(TAG, "jumpCenterPoint>>>>>>>>>>>>>>>>>>>" + centerPointBean.getLng() + "\",\"" + centerPointBean.getLat());
            mWebView.evaluateJavascript("javascript:setCenterAndZoom(\"" + centerPointBean.getLng() + "\",\"" + centerPointBean.getLat() + "\",\"" + centerPointBean.getZoomMp() + "\")", value -> {
            });
        }
        if (mWebView.canGoForward()) {
            mWebView.goForward();
        }
        // 缓存相关
        mWebView.clearCache(false); // 清除缓存
//        mWebView.clearHistory(); // 清除历史
//        mWebView.clearFormData(); // 清除表单数据
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);// 设置缓存模式
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);//开启DOM缓存,关闭的话H5自身的一些操作是无效的
// 缓存模式
//        LOAD_DEFAULT: 默认,根据 cache-control 决定是否从网络上取数据
//        LOAD_NORMAL: API level 17 中已经废弃, 从API level 11开始作用同 LOAD_DEFAULT 模式
//        LOAD_CACHE_ELSE_NETWORK: 只要本地有,无论是否过期,或者 no-cache,都使用缓存中的数据
//        LOAD_NO_CACHE: 不使用缓存,只从网络获取数据
//        LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据
        // js 相关
        webSettings.setJavaScriptEnabled(true); // 支持 js。如果碰到后台无法释放 js 导致耗电,应在 onStop 和 onResume 里分别设成 false 和 true
       //        mWebView.addJavascriptInterface(new WebAppInterface(this), "android"); // js 接口
    }
        //        webSettings.setPluginsEnabled(true); // 支持插件
        // 设置自适应屏幕,两者合用
        webSettings.setUseWideViewPort(true); // 将图片调整到适合 WebView 的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        // 缩放操作
        webSettings.setSupportZoom(true); // 支持缩放,默认为 true
        webSettings.setBuiltInZoomControls(true); // 设置内置的缩放控件,若为 false,则该 WebView 不可缩放
        webSettings.setDisplayZoomControls(false); // 隐藏原生的缩放控件
        webSettings.setLoadsImagesAutomatically(true);//预加载
        //webView弹JsAlert
        mWebView.setWebChromeClient(new WebChromeClient() {
    /**
     * 添加标注点
     */
    public void setMapMarker() {
        Log.i("mWebView", "addMarker????????????" + strMarkerJson);
        mWebView.evaluateJavascript("javascript:addMarker(\"" + strMarkerJson + "\")", new ValueCallback<String>() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            public void onReceiveValue(String value) {
                Log.i("mWebView", "addMarker!!!!!!!!!" + value);
            }
        });
        MyWebViewInterface myWebViewInterface=new MyWebViewInterface(this);
        mWebView.addJavascriptInterface(myWebViewInterface,"Android");
    }
    /**
     * 获取取水口列表
     */
    private void getMarkerData() {
        ApiManager.getInstance().requestGetHideLoading(this.getContext(), Constants.BASE_URL + ":8085/project/intake/all", MarkerResult.class, null, new SubscriberListener<BaseResponse<MarkerResult>>() {
            @Override
            public void onNext(BaseResponse<MarkerResult> t) {
                if (t.isSuccess()) {
                    if (t.isSuccess()) {
                        if (t.getContent().getObj() != null && !t.getContent().getObj().isEmpty()) {
                            List<MarkerBean> markerBeans = new ArrayList<>();
                            for (int i = 0; i < t.getContent().getObj().size(); i++) {
                                MarkerResult.Obj result = t.getContent().getObj().get(i);
                                //保存数据
                                MarkerBean markerBean = new MarkerBean();
                                markerBean.setLng(result.getLng());
                                markerBean.setLat(result.getLat());
                                markerBean.setBlockId(result.getBlockId());
                                markerBean.setName(result.getName());
                                markerBean.setRemarks(result.getRemarks());
                                markerBean.setTownId(result.getTownId());
                                markerBean.setVillageId(result.getVillageId());
                                markerBean.setCountyId(result.getCountyId());
                                markerBeans.add(markerBean);
                            }
                            strMarkerJson = WebViewUtils.beanToJson(markerBeans);
                            setMapMarker();
                            DaoSingleton.getInstance(MapFragment.this.getContext()).markerDao().deleteAll();
                            // 使用 RxJava 异步插入数据
                            DaoSingleton.getAsynchInstance(MapFragment.this.getContext()).markerDao().insertAll(markerBeans)
                                    .subscribeOn(Schedulers.io()) // 在 IO 线程上执行
                                    .observeOn(AndroidSchedulers.mainThread()) // 在主线程上观察
                                    .subscribe(() -> {
                                        // 插入成功
                                        Log.i("mWebView", "数据插入成功");
                                    }, throwable -> {
                                        // 插入失败
                                        Log.e("mWebView", "数据插入失败: " + throwable.getMessage());
                                    });
                        }
                    }
                } else {
                    ToastUtil.showToast(MapFragment.this.getContext(), t.getMsg());
                }
            }
        });
    }
    /**
     * 添加标注点
     */
    public void addMarker() {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        // 中国经纬度范围
        double minLongitude = 73.43;
        double maxLongitude = 135.05;
        double minLatitude = 3.52;
        double maxLatitude = 53.57;
        for (int i = 0; i < 1000; i++) {
            stringBuilder.append("[");
            // 生成随机经度
            double longitude = minLongitude + (maxLongitude - minLongitude) * random.nextDouble();
            stringBuilder.append(longitude);
            stringBuilder.append(",");
            // 生成随机纬度
            double latitude = minLatitude + (maxLatitude - minLatitude) * random.nextDouble();
            stringBuilder.append(latitude);
            stringBuilder.append(",\"237取水口\"],");
        }
        stringBuilder.append("[116.417854,39.921988,\"235取水口\"]]");
        String jsonData = stringBuilder.toString().replace("\\", "\\\\").replace("\"", "\\\"");
        Log.i("mWebView", "addMarker????????????" + jsonData);
        mWebView.evaluateJavascript("javascript:addMarker(\"" + jsonData + "\")", new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                Log.i("mWebView", "addMarker!!!!!!!!!" + value);
            }
        });
    }
    void initView() {
        //跳转到指定位置
        binding.flyBtn.setOnClickListener(v -> {
            mWebView.evaluateJavascript("javascript:locationOverLay(116.399565,39.89432)", value -> {
            });
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                Log.i(TAG, "加载地址>>>" + url);
                //判断当前是否为加载瓦片
                if (MapJpgUtils.getInsatance().isTianDiTuTileRequest(url)) {
                    String androidUrl = url.replace(CommonData.webKey, CommonData.androidKey);
                    // 检查本地缓存
                    File cachedTile = MapJpgUtils.getInsatance().getCachedTile(androidUrl);
                    if (cachedTile != null && cachedTile.exists()) {
                        Log.i(TAG, "本地缓存>>>" + androidUrl);
//                        if (MapJpgUtils.getInsatance().validateImageFile(androidUrl,request.))
                        // 判断缓存是否过期
//                    if (!MapJpgUtils.getInsatance(MapFragment.this.getContext()).isCacheExpired(cachedTile)) {
                        try {
                            // 从缓存加载瓦片
                            return new WebResourceResponse("image/jpg", "utf-8", new FileInputStream(cachedTile));
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
//                    }
                    } else {
                        //下载瓦片
                        ApiManager.getInstance().donwLoadTile(androidUrl);
                    }
                    Log.i(TAG, "在线加载>>>" + url);
                }
                return super.shouldInterceptRequest(view, request);
            }
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                // 捕获加载过程中发生的错误
                int errorCode = error.getErrorCode();
                String description = error.getDescription().toString();
                String failingUrl = request.getUrl().toString();
                Log.e("setWebViewClient", "errorCode:" + errorCode + ">>>>description:" + description + ">>>>failingUrl:" + failingUrl);
                // 处理错误,例如显示错误页面或提示用户
                // view.loadUrl("file:///android_asset/error.html");
            }
            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                // 捕获HTTP错误(如404, 500等)
                int statusCode = errorResponse.getStatusCode();
                String description = errorResponse.getReasonPhrase();
                Log.e("setWebViewClient", "statusCode:" + statusCode + ">>>>description:" + description);
                // 根据HTTP状态码处理错误
            }
        });
        //巡检按钮
        binding.inspectButton.setOnClickListener(v -> {
            if (isStartInspec){
                ConfirmDialog confirmDialog=new ConfirmDialog(MapFragment.this.getActivity(), "提示", "确认关闭巡检吗?", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isStartInspec = !isStartInspec;
                        chageInspecState();
                    }
                });
            }else {
                isStartInspec = !isStartInspec;
                chageInspecState();
            }
        });
    }
    public void showWaterIntakeDetail(String data){
    public void showWaterIntakeDetail(String data) {
        MyLog.i(data);
        binding.bottomLL.setVisibility(View.VISIBLE);
    }
    /**
     * 获取地图中心点
     */
    private void getCenterPoint() {
    public void closeWaterIntakeDetail(){
        ApiManager.getInstance().requestGet(this.getContext(), Constants.BASE_URL + ":8080/base/dict_item/map_center", CenterPointResult.class, null, new SubscriberListener<BaseResponse<CenterPointResult>>() {
            @Override
            public void onNext(BaseResponse<CenterPointResult> t) {
                if (t.isSuccess()) {
                    if (centerPointBean == null) {
                        centerPointBean = new CenterPointBean();
                    }
                    centerPointBean.setLat(t.getContent().getLat());
                    centerPointBean.setLng(t.getContent().getLng());
                    centerPointBean.setZoomMp(t.getContent().getZoomMp());
                    DaoSingleton.getInstance(MapFragment.this.getContext()).centerPointDao().insert(centerPointBean);
                    jumpCenterPoint();
                } else {
                    ToastUtil.showToast(MapFragment.this.getContext(), t.getMsg());
                }
            }
        });
    }
    public void closeWaterIntakeDetail() {
        binding.bottomLL.setVisibility(View.GONE);
    }
}