需要完成效果:集成高德地图,实现定位功能,并且实时刷新位置,每25秒自动刷新位置。
布局:
<?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.administrator.jreduch08.WebViewActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/wv"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"></WebView>
<ProgressBar
android:layout_width="100dp"
android:layout_height="100dp"
style="@android:style/Widget.ProgressBar.Large"
android:id="@+id/pb"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
代码:
package com.example.administrator.jreduch08;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebViewActivity extends AppCompatActivity
implements LocationInfo.IfloadWebView{
private WebView wv;
private ProgressBar pb;
private LocationInfo locationInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
wv= (WebView) findViewById(R.id.wv);
pb= (ProgressBar) findViewById(R.id.pb);
WebSettings set=wv.getSettings();
set.setJavaScriptEnabled(true);
set.setSupportZoom(true);
set.setDisplayZoomControls(true);
wv.setWebViewClient(new WebViewClient());
//在自己写的页面打开网页
wv.setWebChromeClient(new MyWebChromeClient());
locationInfo=new LocationInfo(this);
locationInfo.getLocation();
}
@Override
public void loadLocation(Location location) {
wv.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);
if(newProgress==100){
Log.d("====Progress",""+newProgress);
pb.setVisibility(view.GONE);
}
}
}
}
新建类:
package com.example.administrator.jreduch08;
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 Administrator on 2016/9/7.
*/
public class LocationInfo {
private Context context;
private LocationManager lm;
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) {
return null;
}
lm.requestLocationUpdates(provider, 5000*5, 0f, locationListener);
Location location=lm.getLastKnownLocation(provider);
return location;
}
private final LocationListener locationListener=new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//位置发生改变时
onLocationChange(location);
// 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 void onLocationChange(Location location){
if(location!=null){
Log.d("=====纬度",""+location.getLatitude());
Log.d("=====经度",""+location.getLongitude());
ifloadWebView.loadLocation(location);
}else {
Log.d("====","获取定位失败");
}
}
public interface IfloadWebView{
public void loadLocation(Location location);
}
}
实现效果: