管灌系统巡查员智能手机App
zuoxiao
2024-09-24 45e69852f43abe0f79967786ada3c7cf887b5b48
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,12 +17,21 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.dayu.pipirrapp.bean.db.CenterPointBean;
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.MapJpgUtils;
import com.dayu.pipirrapp.utils.MyLog;
import com.dayu.pipirrapp.utils.ToastUtil;
import com.dayu.pipirrapp.utils.WebViewUtils;
import java.io.File;
@@ -38,11 +46,15 @@
 * 备注:地图页
 */
public class MapFragment extends BaseFragment {
    static String TAG = "MapFragment";
    FragmentMapBinding binding;
    //定位相关
    LocationManager locationManager;
    WebView mWebView;
    CenterPointBean centerPointBean;
    String jsonData;
    @Override
    public void onAttach(@NonNull Context context) {
@@ -66,14 +78,78 @@
        MyWebViewInterface myWebViewInterface = new MyWebViewInterface(MapFragment.this);
        mWebView.addJavascriptInterface(myWebViewInterface, "Android");
        mWebView.loadUrl("file:///android_asset/index.html");
        getCenterPoint();
        initView();
        initData();
        getMarkerData();
        //开启定位
        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() {
        jumpCenterPoint();
    }
    /**
     * 跳转地图中心点
     */
    public void jumpCenterPoint() {
        centerPointBean = DaoSingleton.getInstance(MapFragment.this.getContext()).centerPointDao().findFirst();
        if (centerPointBean != null) {
            Log.d(TAG, "jumpCenterPoint>>>>>>>>>>>>>>>>>>>" + centerPointBean.getLng() + "\",\"" + centerPointBean.getLat());
            mWebView.evaluateJavascript("javascript:setCenterAndZoom(\"" + centerPointBean.getLng() + "\",\"" + centerPointBean.getLat() + "\")", value -> {
            });
        }
    }
    public void setMapMarker(){
        Log.i("mWebView", "addMarker????????????" + jsonData);
        mWebView.evaluateJavascript("javascript:addMarker(\"" + jsonData + "\")", new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                Log.i("mWebView", "addMarker!!!!!!!!!" + value);
            }
        });
    }
    private void getMarkerData() {
        ApiManager.getInstance().requestGet(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()) {
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append("[");
                            for (int i = 0; i < t.getContent().getObj().size(); i++) {
                                stringBuilder.append("[");
                                stringBuilder.append(t.getContent().getObj().get(i).getLng());
                                stringBuilder.append(",");
                                stringBuilder.append(t.getContent().getObj().get(i).getLat());
                                stringBuilder.append(",\"" + t.getContent().getObj().get(i).getName() + "\"]");
                                if (i != t.getContent().getObj().size() - 1) {
                                    stringBuilder.append(",");
                                }
                            }
                            stringBuilder.append("]");
                            jsonData = stringBuilder.toString().replace("\\", "\\\\").replace("\"", "\\\"");
                        }
                    }
                } else {
                    ToastUtil.showToast(MapFragment.this.getContext(), t.getMsg());
                }
            }
        });
    }
    /**
     * 添加标注点
@@ -102,7 +178,6 @@
        }
        stringBuilder.append("[116.417854,39.921988,\"235取水口\"]]");
        String jsonData = stringBuilder.toString().replace("\\", "\\\\").replace("\"", "\\\"");
//        String jsonData = "[[116.417854,39.921988,\"235取水口\"],[116.406605,39.921585,\"237取水口\"],[116.412222,39.912345,\"236取水口\"]]";
        Log.i("mWebView", "addMarker????????????" + jsonData);
        mWebView.evaluateJavascript("javascript:addMarker(\"" + jsonData + "\")", new ValueCallback<String>() {
            @Override
@@ -111,7 +186,6 @@
            }
        });
    }
    @Override
    public void onResume() {
@@ -144,22 +218,31 @@
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public WebResourceResponse  shouldInterceptRequest(WebView view, WebResourceRequest request) {
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                // 检查本地缓存
                File cachedTile = MapJpgUtils.getInsatance(MapFragment.this.getContext()).getCachedTile(url);
                if (cachedTile != null && cachedTile.exists()) {
                    // 判断缓存是否过期
                    if (!MapJpgUtils.getInsatance(MapFragment.this.getContext()).isCacheExpired(cachedTile)) {
                //判断当前是否为加载瓦片
                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);
            }
@@ -194,6 +277,30 @@
    }
    /**
     * 获取地图中心点
     */
    private void getCenterPoint() {
        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());
                    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);
    }