Android——WebView地圖回調刷新位置+接口回調

Android——WebView地圖回調刷新位置+接口回調



[java] view plain copy
  1. <span style="font-size:14px;">package com.example.dell.jreduch07.util;  
  2.   
  3. import android.Manifest;  
  4. import android.content.Context;  
  5. import android.content.pm.PackageManager;  
  6. import android.location.Address;  
  7. import android.location.Criteria;  
  8. import android.location.Geocoder;  
  9. import android.location.Location;  
  10. import android.location.LocationListener;  
  11. import android.location.LocationManager;  
  12. import android.os.Bundle;  
  13. import android.support.v4.app.ActivityCompat;  
  14. import android.util.Log;  
  15.   
  16. import java.io.IOException;  
  17. import java.util.List;  
  18. import java.util.Locale;  
  19.   
  20. /** 
  21.  * Created by 沖天之峯 on 2016/9/7. 
  22.  */  
  23. public class LocationInfo {  
  24.   
  25.     private Context context;  
  26.     private LocationManager lm;  
  27. private  Location location2;  
  28.   
  29.   
  30.  private  IfloadWebView ifloadWebView;  
  31.     public LocationInfo(Context context) {  
  32.         this.context = context;  
  33.         ifloadWebView= (IfloadWebView) context;  
  34.   
  35.   
  36.     }  
  37.   
  38.     public Location getLocation() {  
  39.         lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);  
  40.         Criteria criteria = new Criteria();  
  41.         criteria.setAccuracy(Criteria.ACCURACY_FINE);//定位精度  
  42.         criteria.setAltitudeRequired(false);//是否需要海拔信息;  
  43.         criteria.setPowerRequirement(Criteria.POWER_LOW);  //功耗  
  44.   
  45.         String provider = lm.getBestProvider(criteria, true);  //最好功能提供給服務者  
  46.         if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {  
  47.             // TODO: Consider calling  
  48.             //    ActivityCompat#requestPermissions  
  49.             // here to request the missing permissions, and then overriding  
  50.             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,  
  51.             //                                          int[] grantResults)  
  52.             // to handle the case where the user grants the permission. See the documentation  
  53.             // for ActivityCompat#requestPermissions for more details.  
  54.             return null;  
  55.         }  
  56.         lm.requestLocationUpdates(provider, 4000 * 10,0f, locationListener);//米  
  57.        Location location= lm.getLastKnownLocation(provider);  
  58.         return  location;  
  59.   
  60.     }  
  61.     private  final LocationListener locationListener=new LocationListener() {  
  62.         @Override  
  63.         public void onLocationChanged(Location location) {  
  64.             if (location!=null){  
  65.                 Log.d("=======緯度",""+location.getLatitude());  
  66.                 Log.d("=======經度",""+location.getLongitude());  
  67. //              location2=location;  
  68. //                if (location2!=location) {  
  69.                     ifloadWebView.loadLocation(location);  
  70. //                }  
  71.             }else {  
  72.   
  73.                 Log.d("=======緯度","定位失敗");  
  74.             }  
  75. getAddress(location);  
  76.         }  
  77.   
  78.         @Override  
  79.         public void onStatusChanged(String provider, int status, Bundle extras) {  
  80.   
  81.         }  
  82.   
  83.         @Override  
  84.         public void onProviderEnabled(String provider) {  
  85.   
  86.         }  
  87.   
  88.         @Override  
  89.         public void onProviderDisabled(String provider) {  
  90.   
  91.         }  
  92.     };  
  93.   
  94.   
  95.   
  96.   
  97.     public List<Address> getAddress(Location location){  
  98.         List<Address> result=null;  
  99.         if (location!=null){  
  100.             Geocoder gc=new Geocoder(context, Locale.getDefault());  
  101.             try {  
  102.               result=  gc.getFromLocation(location.getLatitude()  
  103.                         ,location.getLongitude(),10);  
  104.                 for (Address s:result){  
  105.                     Log.d("=======++++中國",s.getCountryName());  
  106.                  //  Log.d("=======++++",s.getFeatureName());  
  107.                    Log.d("=======++++山東省",s.getAdminArea());  
  108.                     Log.d("=======++++煙臺市",s.getLocality());  
  109.                    // Log.d("=======++++",s.getSubAdminArea());  
  110.                  //   Log.d("=======++++",s.getPremises());  
  111.                     Log.d("=======++++","================");  
  112.                 }  
  113.   
  114.             } catch (IOException e) {  
  115.                 e.printStackTrace();  
  116.             }  
  117.   
  118.         }  
  119.         return  result;  
  120.     }  
  121.   
  122.     public interface  IfloadWebView{  
  123.         public  void  loadLocation(Location location);  
  124.   
  125.   
  126.     }  
  127.   
  128.   
  129.   
  130. }</span><span style="font-size:24px;">  
  131. </span>  
[java] view plain copy
  1. package com.example.dell.jreduch07;  
  2.   
  3. import android.location.Location;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.webkit.WebChromeClient;  
  9. import android.webkit.WebSettings;  
  10. import android.webkit.WebView;  
  11. import android.webkit.WebViewClient;  
  12. import android.widget.ProgressBar;  
  13.   
  14. import com.example.dell.jreduch07.util.LocationInfo;  
  15.   
  16. public class WebViewActivity extends AppCompatActivity implements LocationInfo.IfloadWebView{  
  17.     private WebView wv1;  
  18.     private ProgressBar pb;  
  19.     private LocationInfo locationInfo;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_web_view);  
  25.         pb=(ProgressBar)findViewById(R.id.pb);  
  26.         wv1=(WebView)findViewById(R.id.wv1);  
  27.         WebSettings ws=wv1.getSettings();  
  28.         //縮放控件  
  29.         ws.setDisplayZoomControls(true);  
  30.         ws.setSupportZoom(true);  
  31.         //支持縮放  
  32.         ws.setJavaScriptEnabled(true);  
  33.         wv1.setWebViewClient(new WebViewClient());//否則在手機自帶瀏覽器打開  
  34.       //  wv1.setWebChromeClient(new WebChromeClient());  
  35.         wv1.setWebChromeClient(new MyWebChromeClient());  
  36.        // wv1.loadUrl("http://www.baidu.com");  
  37.        // wv1.loadUrl("http://m.amap.com/?q=31.234527,121.287689");  
  38.        // wv1.loadUrl("http://m.amap.com/?q=37.466975,121.436982");  
  39.   
  40.        locationInfo=new LocationInfo(this);  
  41.       Location location= locationInfo.getLocation();  
  42. //        if (location!=null){  
  43. //            wv1.loadUrl("http://m.amap.com/?q="  
  44. //                    +""+location.getLatitude()+","+location.getLongitude());  
  45. //  
  46. //        }else {  
  47. //            wv1.loadUrl("http://m.amap.com/?q=31.234527,121.287689");  
  48. //        }  
  49.   
  50.     }  
  51.   
  52.     @Override  
  53.     public void loadLocation(Location location) {  
  54.         wv1.loadUrl("http://m.amap.com/?q="  
  55.                 +""+location.getLatitude()+","+location.getLongitude());  
  56.   
  57.     }  
  58.   
  59.     public  class MyWebChromeClient extends WebChromeClient{  
  60.   
  61.         @Override  
  62.         public void onProgressChanged(WebView view, int newProgress) {  
  63.             super.onProgressChanged(view, newProgress);  
  64.   
  65.             Log.d("=====newProgress:",""+newProgress);  
  66.             if (newProgress==100){  
  67.                 pb.setVisibility(View.GONE);  
  68.   
  69.             }  
  70.         }  
  71.     }  
  72.   
  73.   
  74. }  

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.   
  7.     tools:context="com.example.dell.jreduch07.WebViewActivity">  
  8.   
  9.     <WebView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:id="@+id/wv1"  
  13.         android:layout_centerVertical="true"  
  14.         android:layout_centerHorizontal="true"></WebView>  
  15.   
  16.     <ProgressBar  
  17.         android:id="@+id/pb"  
  18.         android:layout_width="100dp"  
  19.         android:layout_height="100dp"  
  20.         style="@android:style/Widget.ProgressBar.Large"  
  21.         android:layout_centerVertical="true"  
  22.         android:layout_centerHorizontal="true" />  
  23. </RelativeLayout>  







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章