我的应用将在当前位置显示标记。 GPS设置警报对话框将显示GPS是否被禁用。启用GPS并返回到应用程序后,地图不会刷新。所以我必须强制停止我的应用程序并再次打开。如何刷新地图活动?启用GPS设置后刷新地图以显示当前位置
GoogleMap googleMap;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
ViewMap();
} catch (Exception e) {
e.printStackTrace();
}
}
private void ViewMap() {
if(googleMap == null) {
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng latLng = new LatLng(latitude,longitude);
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
else {
gps.showSettingsAlert();
}
}
}
@Override
protected void onResume() {
super.onResume();
ViewMap();
}
覆盖上的位置变化的方法。它会给更新的lat long值。 – Vid