| | |
| | | package com.dayu.pipirrapp.fragment; |
| | | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.location.LocationManager; |
| | | 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.view.WindowManager; |
| | | import android.webkit.JsResult; |
| | | import android.webkit.WebChromeClient; |
| | | import android.webkit.WebSettings; |
| | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | import androidx.lifecycle.DefaultLifecycleObserver; |
| | | import androidx.lifecycle.Lifecycle; |
| | | import androidx.lifecycle.LifecycleEventObserver; |
| | | import androidx.lifecycle.LifecycleObserver; |
| | | import androidx.lifecycle.LifecycleOwner; |
| | | |
| | | import com.dayu.pipirrapp.databinding.FragmentMapBinding; |
| | | import com.dayu.pipirrapp.js.MyWebViewInterface; |
| | | import com.dayu.pipirrapp.observer.MapFragmenObserver; |
| | | import com.dayu.pipirrapp.service.MyLocationService; |
| | | import com.dayu.pipirrapp.utils.MyLog; |
| | | import com.dayu.pipirrapp.utils.ToastUtil; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * author: zuo |
| | |
| | | * Time: 10:16 |
| | | * 备注:地图页 |
| | | */ |
| | | public class MapFragment extends BaseFragment implements DefaultLifecycleObserver { |
| | | public class MapFragment extends BaseFragment { |
| | | |
| | | FragmentMapBinding binding; |
| | | LocationManager locationManager; |
| | |
| | | @Override |
| | | public void onAttach(@NonNull Context context) { |
| | | super.onAttach(context); |
| | | requireActivity().getLifecycle().addObserver(this); |
| | | requireActivity().getLifecycle().addObserver(new MapFragmenObserver()); |
| | | } |
| | | |
| | | @Override |
| | | public void onCreate(@Nullable Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); |
| | | } |
| | | |
| | | @Nullable |
| | | @Override |
| | | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| | | |
| | | binding = FragmentMapBinding.inflate(inflater, container, false); |
| | | Log.i("MapFragment", "onCreateView"); |
| | | mWebView = binding.webView; |
| | | initWebView(); |
| | | mWebView.loadUrl("http://192.168.3.178:3002/"); |
| | | mWebView.loadUrl("http://192.168.3.178:3000/"); |
| | | initView(); |
| | | //开启定位 |
| | | Intent location = new Intent(this.getActivity(), MyLocationService.class); |
| | | location.putExtra("isSingle", false); |
| | | this.getActivity().startService(location); |
| | | return binding.getRoot(); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onCreate(@NonNull LifecycleOwner owner) { |
| | | DefaultLifecycleObserver.super.onCreate(owner); |
| | | |
| | | Log.i("MapFragment", "onCreate"); |
| | | } |
| | | |
| | | @Override |
| | | public void onResume() { |
| | |
| | | // LOAD_CACHE_ELSE_NETWORK: 只要本地有,无论是否过期,或者 no-cache,都使用缓存中的数据 |
| | | // LOAD_NO_CACHE: 不使用缓存,只从网络获取数据 |
| | | // LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据 |
| | | // js 相关 |
| | | // js 相关 |
| | | webSettings.setJavaScriptEnabled(true); // 支持 js。如果碰到后台无法释放 js 导致耗电,应在 onStop 和 onResume 里分别设成 false 和 true |
| | | // mWebView.addJavascriptInterface(new WebAppInterface(this), "android"); // js 接口 |
| | | // mWebView.addJavascriptInterface(new WebAppInterface(this), "android"); // js 接口 |
| | | |
| | | // webSettings.setPluginsEnabled(true); // 支持插件 |
| | | // 设置自适应屏幕,两者合用 |
| | | // 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(){ |
| | | @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { |
| | | mWebView.setWebChromeClient(new WebChromeClient() { |
| | | @Override |
| | | public boolean onJsAlert(WebView view, String url, String message, JsResult result) { |
| | | return super.onJsAlert(view, url, message, result); |
| | | } |
| | | }); |
| | | |
| | | MyWebViewInterface myWebViewInterface=new MyWebViewInterface(this); |
| | | mWebView.addJavascriptInterface(myWebViewInterface,"Android"); |
| | | } |
| | | |
| | | void initView(){ |
| | | binding.flyBtn.setOnClickListener(v->{ |
| | | mWebView.evaluateJavascript("javascript:data()",value -> { |
| | | |
| | | void initView() { |
| | | binding.flyBtn.setOnClickListener(v -> { |
| | | mWebView.evaluateJavascript("javascript:locationOverLay(116.399565,39.89432)", value -> { |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | |
| | | public void showWaterIntakeDetail(String data){ |
| | | MyLog.i(data); |
| | | binding.bottomLL.setVisibility(View.VISIBLE); |
| | | } |
| | | |
| | | |
| | | |
| | | public void closeWaterIntakeDetail(){ |
| | | binding.bottomLL.setVisibility(View.GONE); |
| | | } |
| | | |
| | | } |