我有一个实现Google Map的Activity。当我启动它时,活动会停止几秒钟,直到Map完全加载。 我想使用ProgressDialog,直到地图没有加载,但我无法在后台线程中启动它,因为必须在主线程中加载地图,如this link中所述。
如何在不使用AsyncTask的情况下制作它?
否则,有没有办法立即启动活动,并像谷歌地图应用程序一样显示带灰色背景的未加载地图?
这是onCreate方法的代码:
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();
}
});
}
答案 0 :(得分:0)
在ProgressDialog not shown in UIThread问题上检查@commonsware的答案。将ProgressDialog用于MapActivity可能不是一个好主意,因为显示MapView所需的时间主要取决于返回到Google Maps服务器的Internet连接。您无法知道MapView何时完成加载,因此您无法知道何时关闭对话框。但是,如果您确定它将始终加载并且花费的时间更少,那么您可以阅读更多here。您可能还希望查看指向相同链接的相关问题:Android : showDialog when setContentView loading
希望这有帮助。
答案 1 :(得分:0)
我终于解决了问题!我使用了this tutorial中的代码。
我所要做的就是在这段代码中加载地图和标记,其中出现“setContentView(R.layout.main);”调用
答案 2 :(得分:0)
请按照以下步骤操作:
1)实现GoogleMap.OnMapLoadedCallback。
当地图准备好使用时的回调界面。
2)onCreate方法中的ProgressDialog代码。
//显示进度
3)onMapReady方法
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "OnMapReady");
mMap = googleMap;
mMap.setOnMapLoadedCallback(this);
3)加载地图时onMapLoaded()调用。
public void onMapLoaded() {
// hide progress
}
请检查第3点是否重要。