管灌系统巡查员智能手机App
zuoxiao
2024-09-20 13b3e727a6c252887cea8aa6b0856c967f036731
app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
@@ -1,7 +1,6 @@
package com.dayu.pipirrapp.fragment;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
@@ -18,17 +17,28 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.dayu.pipirrapp.MyApplication;
import com.dayu.pipirrapp.bean.db.CenterPointBean;
import com.dayu.pipirrapp.bean.net.CenterPointResult;
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.MapJpgUtils;
import com.dayu.pipirrapp.utils.MyLog;
import com.dayu.pipirrapp.utils.ToastUtil;
import com.dayu.pipirrapp.utils.WebViewUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
@@ -38,11 +48,14 @@
 * 备注:地图页
 */
public class MapFragment extends BaseFragment {
    static String TAG = "MapFragment";
    FragmentMapBinding binding;
    //定位相关
    LocationManager locationManager;
    WebView mWebView;
    CenterPointBean centerPointBean;
    @Override
    public void onAttach(@NonNull Context context) {
@@ -66,14 +79,22 @@
        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);
//        Intent location = new Intent(this.getActivity(), MyLocationService.class);
//        location.putExtra("isSingle", false);
//        this.getActivity().startService(location);
        return binding.getRoot();
    }
    private void initData() {
        centerPointBean = DaoSingleton.getInstance(MapFragment.this.getContext()).centerPointDao().findFirst();
        if (centerPointBean!=null){
        }
    }
    /**
     * 添加标注点
@@ -146,20 +167,29 @@
            @Override
            public WebResourceResponse  shouldInterceptRequest(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                //判断当前是否为加载瓦片
                if (MapJpgUtils.getInsatance().isTianDiTuTileRequest(url)) {
                    String androidUrl = url.replace(CommonData.webKey, CommonData.androidKey);
                // 检查本地缓存
                File cachedTile = MapJpgUtils.getInsatance(MapFragment.this.getContext()).getCachedTile(url);
                    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)) {
//                    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);
            }
@@ -194,6 +224,31 @@
    }
    /**
     * 获取地图中心点
     */
    private void getCenterPoint() {
        Map<String, Object> data = new HashMap<>();
        data.put("token", MyApplication.myApplication.token);
        ApiManager.getInstance().requestGet(this.getContext(), Constants.SUCCESS + "8080/base/dict_item/map_center", CenterPointResult.class, data, 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());
                    DaoSingleton.getInstance(MapFragment.this.getContext()).centerPointDao().update(centerPointBean);
                } else {
                    ToastUtil.showToast(MapFragment.this.getContext(), t.getMsg());
                }
            }
        });
    }
    public void closeWaterIntakeDetail() {
        binding.bottomLL.setVisibility(View.GONE);
    }