Google 地图加载时的进度条
ProgressBar while Google Maps is loading
我从数据库中获取地址,并使用第一个地理编码器在 google 地图 activity 上标记它们,如果 returns LatLng 为空,我使用异步任务从中获取它googleAPI。
该代码运行良好,但在选择 500-600 个地址时,加载地图需要一段时间(15-30 秒,具体取决于数量)。
如何在加载地图时添加圆形进度条?问题是在加载地图时,屏幕是黑色的。我尝试在 asynctask 中添加一个 progressBar 但它不起作用。
即使在加载地图后,asynctask 也会继续向地图添加图钉,您无法知道它何时完成,请记住图钉很多。
我在 onCreate() 中创建了进度条并将其设置为不可见:
progressBar = new ProgressBar(MapsActivity.this, null,android.R.attr.progressBarStyleSmall);
progressBar.setVisibility(View.INVISIBLE);
在 asynctask 中,在 onPreExecute() 上我将其设置为可见:
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
来自here
public abstract void onMapLoaded ()
当地图完成渲染时调用。这只会被调用一次。如果您想再次收到通知,您必须请求另一个回调。
这在 Android UI 线程上调用。
您的问题基本上与用户体验有关,与您的代码无关。
您可以使用两种解决方案告诉用户应用程序正在做某事,而不仅仅是冻结或崩溃:
解决方案 1
您可以在加载信息时使用显示的加载视图代替地图
<FrameLayout
<!-- same layout information as your current MapView -->
...
>
<MapView
...
/>
<View
<!-- This can be a ProgressBar or a custom loading View with animation -->
/>
</FrameLayout>
然后您可以在加载数据时创建视图 Visible
,然后在完成后显示地图。这将有助于避免黑屏
Update
Following this answer, you can find to extend a Fragment
and add a MapView
正在设置显示地图的布局。 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>
现在我们编写 java class 代码来显示地图。 MapViewFragment :
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();
}
}
这样您就可以 hide/show loadingView
每当您完成获取数据时。
解决方案 2
您可以在加载数据之前仅显示地图,然后在获取数据时在其顶部显示 ProgressBar
。加载数据后,您可以填充地图。
我从数据库中获取地址,并使用第一个地理编码器在 google 地图 activity 上标记它们,如果 returns LatLng 为空,我使用异步任务从中获取它googleAPI。
该代码运行良好,但在选择 500-600 个地址时,加载地图需要一段时间(15-30 秒,具体取决于数量)。
如何在加载地图时添加圆形进度条?问题是在加载地图时,屏幕是黑色的。我尝试在 asynctask 中添加一个 progressBar 但它不起作用。
即使在加载地图后,asynctask 也会继续向地图添加图钉,您无法知道它何时完成,请记住图钉很多。
我在 onCreate() 中创建了进度条并将其设置为不可见:
progressBar = new ProgressBar(MapsActivity.this, null,android.R.attr.progressBarStyleSmall);
progressBar.setVisibility(View.INVISIBLE);
在 asynctask 中,在 onPreExecute() 上我将其设置为可见:
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
来自here
public abstract void onMapLoaded ()
当地图完成渲染时调用。这只会被调用一次。如果您想再次收到通知,您必须请求另一个回调。
这在 Android UI 线程上调用。
您的问题基本上与用户体验有关,与您的代码无关。
您可以使用两种解决方案告诉用户应用程序正在做某事,而不仅仅是冻结或崩溃:
解决方案 1
您可以在加载信息时使用显示的加载视图代替地图
<FrameLayout
<!-- same layout information as your current MapView -->
...
>
<MapView
...
/>
<View
<!-- This can be a ProgressBar or a custom loading View with animation -->
/>
</FrameLayout>
然后您可以在加载数据时创建视图 Visible
,然后在完成后显示地图。这将有助于避免黑屏
Update
Following this answer, you can find to extend aFragment
and add aMapView
正在设置显示地图的布局。 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>
现在我们编写 java class 代码来显示地图。 MapViewFragment :
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();
}
}
这样您就可以 hide/show loadingView
每当您完成获取数据时。
解决方案 2
您可以在加载数据之前仅显示地图,然后在获取数据时在其顶部显示 ProgressBar
。加载数据后,您可以填充地图。