Android——WebView地图回调刷新位置+接口回调
- <span style="font-size:14px;">package com.example.dell.jreduch07.util;
- import android.Manifest;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.location.Address;
- import android.location.Criteria;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.support.v4.app.ActivityCompat;
- import android.util.Log;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- /**
- * Created by 冲天之峰 on 2016/9/7.
- */
- public class LocationInfo {
- private Context context;
- private LocationManager lm;
- private Location location2;
- private IfloadWebView ifloadWebView;
- public LocationInfo(Context context) {
- this.context = context;
- ifloadWebView= (IfloadWebView) context;
- }
- public Location getLocation() {
- lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
- Criteria criteria = new Criteria();
- criteria.setAccuracy(Criteria.ACCURACY_FINE);//定位精度
- criteria.setAltitudeRequired(false);//是否需要海拔信息;
- criteria.setPowerRequirement(Criteria.POWER_LOW); //功耗
- String provider = lm.getBestProvider(criteria, true); //最好功能提供给服务者
- if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return null;
- }
- lm.requestLocationUpdates(provider, 4000 * 10,0f, locationListener);//米
- Location location= lm.getLastKnownLocation(provider);
- return location;
- }
- private final LocationListener locationListener=new LocationListener() {
- @Override
- public void onLocationChanged(Location location) {
- if (location!=null){
- Log.d("=======纬度",""+location.getLatitude());
- Log.d("=======经度",""+location.getLongitude());
- // location2=location;
- // if (location2!=location) {
- ifloadWebView.loadLocation(location);
- // }
- }else {
- Log.d("=======纬度","定位失败");
- }
- getAddress(location);
- }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- @Override
- public void onProviderEnabled(String provider) {
- }
- @Override
- public void onProviderDisabled(String provider) {
- }
- };
- public List<Address> getAddress(Location location){
- List<Address> result=null;
- if (location!=null){
- Geocoder gc=new Geocoder(context, Locale.getDefault());
- try {
- result= gc.getFromLocation(location.getLatitude()
- ,location.getLongitude(),10);
- for (Address s:result){
- Log.d("=======++++中国",s.getCountryName());
- // Log.d("=======++++",s.getFeatureName());
- Log.d("=======++++山东省",s.getAdminArea());
- Log.d("=======++++烟台市",s.getLocality());
- // Log.d("=======++++",s.getSubAdminArea());
- // Log.d("=======++++",s.getPremises());
- Log.d("=======++++","================");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- public interface IfloadWebView{
- public void loadLocation(Location location);
- }
- }</span><span style="font-size:24px;">
- </span>
- package com.example.dell.jreduch07;
- import android.location.Location;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.View;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.ProgressBar;
- import com.example.dell.jreduch07.util.LocationInfo;
- public class WebViewActivity extends AppCompatActivity implements LocationInfo.IfloadWebView{
- private WebView wv1;
- private ProgressBar pb;
- private LocationInfo locationInfo;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_web_view);
- pb=(ProgressBar)findViewById(R.id.pb);
- wv1=(WebView)findViewById(R.id.wv1);
- WebSettings ws=wv1.getSettings();
- //缩放控件
- ws.setDisplayZoomControls(true);
- ws.setSupportZoom(true);
- //支持缩放
- ws.setJavaScriptEnabled(true);
- wv1.setWebViewClient(new WebViewClient());//否则在手机自带浏览器打开
- // wv1.setWebChromeClient(new WebChromeClient());
- wv1.setWebChromeClient(new MyWebChromeClient());
- // wv1.loadUrl("http://www.baidu.com");
- // wv1.loadUrl("http://m.amap.com/?q=31.234527,121.287689");
- // wv1.loadUrl("http://m.amap.com/?q=37.466975,121.436982");
- locationInfo=new LocationInfo(this);
- Location location= locationInfo.getLocation();
- // if (location!=null){
- // wv1.loadUrl("http://m.amap.com/?q="
- // +""+location.getLatitude()+","+location.getLongitude());
- //
- // }else {
- // wv1.loadUrl("http://m.amap.com/?q=31.234527,121.287689");
- // }
- }
- @Override
- public void loadLocation(Location location) {
- wv1.loadUrl("http://m.amap.com/?q="
- +""+location.getLatitude()+","+location.getLongitude());
- }
- public class MyWebChromeClient extends WebChromeClient{
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- super.onProgressChanged(view, newProgress);
- Log.d("=====newProgress:",""+newProgress);
- if (newProgress==100){
- pb.setVisibility(View.GONE);
- }
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.dell.jreduch07.WebViewActivity">
- <WebView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/wv1"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"></WebView>
- <ProgressBar
- android:id="@+id/pb"
- android:layout_width="100dp"
- android:layout_height="100dp"
- style="@android:style/Widget.ProgressBar.Large"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
-
-
-
-
-
-