I have an Activity that implements Google Maps.When I start it, the activity stops for a few seconds, until the Map is completely loaded. I would like to use a ProgressDialog until the map does not load, but I can not start it in a background thread, since the map must be loaded in the main thread, as explained in this link.
How can I make it without using the AsyncTask?
Otherwise, is there a way to start the activity immediately and show the not loaded map with the gray background as does the Google Maps application?
That's the code of the onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
databaseHelper = new MyDatabaseHelper(this);
Bundle b = getIntent().getExtras();
String placeAddress= "";
if(b != null)
placeAddress= b.getString("indirizzo");
setUpMapIfNeeded();
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.setMyLocationEnabled(true);
UiSettings settings = gMap.getUiSettings();
settings.setMyLocationButtonEnabled(true);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();
position = new LatLng(lat, lng);
if(!placeAddress.equals("")){
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> indirizzi = null;
try {
indirizzi = geocoder.getFromLocationName(placeAddress, 1);
} catch (IOException e) {
e.printStackTrace();
}
double latLuogo = indirizzi.get(0).getLatitude();
double lngLuogo = indirizzi.get(0).getLongitude();
LatLng luogo = new LatLng(latLuogo, lngLuogo);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(luogo)
.zoom(15)
.build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
else{
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(position)
.zoom(15)
.build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
LocationListener listener = new LocationListener() {
public void onLocationChanged(Location location){
//makeUseOfNewLocation(location);
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String arg0) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
locationManager.requestLocationUpdates(provider, 0, 10, listener);
ConnectivityManager connMngr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = connMngr.getActiveNetworkInfo();
if(checkConnection(netInfo) == true ){
loadFromDatabase(); //load markers
gMap.setInfoWindowAdapter(new InfoWindowAdapter(){
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
String nome = marker.getTitle();
String indirizzo = marker.getSnippet();
View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
TextView title = (TextView)v.findViewById(R.id.titleInfoWindow);
title.setText(nome);
TextView snippet = (TextView)v.findViewById(R.id.snippetInfoWindow);
snippet.setText(indirizzo);
ImageView imView = (ImageView)v.findViewById(R.id.info_windowImageView);
impostaImmagine(imView, nome);
return v;
}
});
gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
final String nome = marker.getTitle();
final String indirizzo = marker.getSnippet();
startLuogoActivity(nome, indirizzo);
}
});
final Geocoder geocoder = new Geocoder(this, Locale.getDefault());
gMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng point) {
List<Address> addresses = null;
if(checkConnection(netInfo) == true){
try {
addresses = geocoder.getFromLocation(point.latitude, point.longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses!=null){
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
String indirizzo = address + ", " + city + ", " + country;
final Dialog addByClickDialog = onCreateDialogADDByClick(getBaseContext(), indirizzo);
addByClickDialog.show();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(50);
}else{
final Dialog nessunaConnessioneDialog = onCreateDialogNessunaConnessione(getBaseContext());
nessunaConnessioneDialog.show();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(50);
}
}
else{
final Dialog nessunaConnessioneDialog = onCreateDialogNessunaConnessione(getBaseContext());
nessunaConnessioneDialog.show();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(50);
}
}
});
Button addButton = (Button)findViewById(R.id.addButton);
final Dialog addDialog;
if(checkConnection(netInfo) == false){
addDialog = onCreateDialogADD(getBaseContext(), false);
}else{
addDialog = onCreateDialogADD(getBaseContext(), true);
}
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
addDialog.show();
}
});
Button deleteButton = (Button)findViewById(R.id.deleteButton);
final Dialog deleteDialog;
if(checkConnection(netInfo) == false){
deleteDialog = onCreateDialogDELETE(getBaseContext(), false);
}else{
deleteDialog = onCreateDialogDELETE(getBaseContext(), true);
}
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteDialog.show();
}
});
}