我从数据库中获取地址,并使用第一个 Geocoder 在谷歌地图 Activity 中标记它们,如果它返回 LatLng null,我使用异步任务从 googleapi 中获取它。
该代码运行良好,但在选择 500-600 个地址时,加载 map 需要一段时间(15-30 秒,具体取决于数量)。
如何在加载 map 时添加圆形进度条?问题是在加载 map 时,屏幕是黑色的。我尝试在 asynctask 中添加一个 progressBar,但它不起作用。
即使在加载 map 后,asynctask 也会继续向 map 添加图钉,您无法知道它何时完成,请记住图钉很多。
我在 onCreate() 中创建了 progressBar 并将其设置为不可见:
progressBar = new ProgressBar(MapsActivity.this, null,android.R.attr.progressBarStyleSmall);
progressBar.setVisibility(View.INVISIBLE);
在 asynctask 中,在 onPreExecute() 上我将其设置为可见:
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
最佳答案
您的问题基本上与用户体验有关,与您的代码无关。
您可以使用两种解决方案告诉用户应用程序正在做某事,而不仅仅是卡住或崩溃:
方案一
您可以在加载信息时使用显示的加载 View 代替 map
<FrameLayout
<!-- same layout information as your current MapView -->
...
>
<MapView
...
/>
<View
<!-- This can be a ProgressBar or a custom loading View with animation -->
/>
</FrameLayout>
然后您可以在加载数据时使 View 可见
,然后在完成后显示 map 。这将有助于避免黑屏
Update
Following this answer, you can find to extend aFragment
and add aMapView
设置显示 map 的布局。 location_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="@+id/loadingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
现在我们编写用于显示 map 的 java 类。 map View fragment :
public class MapViewFragment extends Fragment {
MapView mMapView;
View mLoadingView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.location_fragment, container, false);
mMapView = (MapView) rootView.findViewById(R.id.mapView);
mLoadingView = rootView.findViewById(R.id.loadingView); // you can handle your view as you wish
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
// For showing a move to my location button
googleMap.setMyLocationEnabled(true);
// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
这样你就可以在你完成获取数据时隐藏/显示 loadingView
。
方案二
您可以在加载数据之前仅显示 map ,然后在获取数据时在其顶部显示 ProgressBar
。加载数据后,您可以填充 map 。
关于android - 加载 Google map 时的 ProgressBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922559/