From 45e69852f43abe0f79967786ada3c7cf887b5b48 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期二, 24 九月 2024 10:53:14 +0800
Subject: [PATCH] 从服务端获取取水口坐标并显示在地图上

---
 app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java |  137 ++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 122 insertions(+), 15 deletions(-)

diff --git a/app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java b/app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
index d2bff28..7c122e4 100644
--- a/app/src/main/java/com/dayu/pipirrapp/fragment/MapFragment.java
+++ b/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);
     }

--
Gitblit v1.8.0