在我的片段中,我收到错误java.lang.IllegalStateException:没有包含的点= = 我尝试了很多解决方案,但不起作用
这是我的代码:
public void mSetUpMap() {
/**clear the map before redraw to them*/
googleMap.clear();
/**Create dummy Markers List*/
if (AppUtil.itinerary != null)
str = AppUtil.itinerary.getItinerary();
if (AppUtil.itinerary != null)
shareUrl = AppUtil.itinerary.getShareUrl();
Log.e("Ittt", "" + AppUtil.itinerary.getItinerary());
((HomeActivity) getActivity()).setTexrViewText(str);
poiList.clear();
poiList = AppUtil.itinerary.getPoiList();
final List<Marker> markersList = new ArrayList<>();
for (POI item : poiList) {
Marker m1 = googleMap.addMarker(new MarkerOptions().position(new
LatLng(item.getLatitude(),
item.getLongitude()))
.title(item.getName()).anchor(0.7f, 0.6f)
.icon(BitmapDescriptorFactory.fromBitmap(getCustomMarker
((R.drawable.m2red),
item.getName()))));
markersList.add(m1);
}
builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
/**initialize the padding for map boundary*/
int padding = 200;
/**create the bounds from latlngBuilder to set into map camera*/
LatLngBounds bounds = builder.build();
/**create the camera with bounds and padding to set into map*/
cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
/**call the map call back to know map is loaded or not*/
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
/**set animated zoom camera into map*/
googleMap.animateCamera(cu);
PolylineOptions polylineOptions = new PolylineOptions();
for (POI item : poiList) {
polylineOptions.add(new LatLng(item.getLatitude(),
item.getLongitude()));
}
polylineOptions.width(5);
polylineOptions.getPoints();
polylineOptions.color(getResources().getColor(R.color.red));
googleMap.addPolyline(polylineOptions);
polylineOptions.geodesic(true);
if (!poiList.isEmpty()) {
// Adjusting Bounds
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
CameraUpdate mCameraUpdate =
CameraUpdateFactory.newLatLngBounds(bounds, 2);
googleMap.animateCamera(mCameraUpdate);
}
}
});
googleMap.setOnMarkerClickListener(new
GoogleMap.OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(final Marker marker) {
ValueAnimator ani = ValueAnimator.ofFloat(0, 1); //change for
(0,1) if you want a fade in
ani.setDuration(2000);
ani.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation) {
marker.setAlpha((float) animation.getAnimatedValue());
}
});
ani.start();
if (marker.getTitle().equals(poiList.get(0).getName())) {
AppUtil.poi = poiList.get(0);
name = poiList.get(0).getName();
img = poiList.get(0).getImage();
lat = poiList.get(0).getLatitude();
lon = poiList.get(0).getLongitude();
showPoi(name, img, lat, lon);
} else if (marker.getTitle().equals(poiList.get(1).getName())) {
AppUtil.poi = poiList.get(1);
name = poiList.get(1).getName();
img = poiList.get(1).getImage();
lat = poiList.get(1).getLatitude();
lon = poiList.get(1).getLongitude();
showPoi(name, img, lat, lon);
} else if (marker.getTitle().equals(poiList.get(2).getName())) {
AppUtil.poi = poiList.get(2);
name = poiList.get(2).getName();
img = poiList.get(2).getImage();
lat = poiList.get(2).getLatitude();
lon = poiList.get(2).getLongitude();
showPoi(name, img, lat, lon);
}
return true;
}
});
}
我收到错误 LatLngBounds bounds = builder.build((; 这条线
第一次片段工作完美,但是当我按下后退按钮并尝试获取此片段时,我收到此错误
我的日志猫秀:
java.lang.IllegalStateException: no included points
at com.google.android.gms.common.internal.zzac.zza(Unknown Source:8)
at com.google.android.gms.maps.model.LatLngBounds$Builder.build(Unknown
Source:10)
at
info.ernica.fragment.ItinerarioFragment.mSetUpMap
(ItinerarioFragment.java:455)
at info.ernica.fragment.ItinerarioFragment$2.onMapReady
(ItinerarioFragment.java:205)
at com.google.android.gms.maps.MapFragment$zza$1.zza(Unknown Source:7)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown
Source:32)
at android.os.Binder.transact(Binder.java:612)
at fg.b(:com.google.android.gms.dynamite_mapsdynamite@13280051@13.2.80
(040408-211705629):19)
at
com.google.android.gms.maps.internal.bg.a
(:com.google.android.gms.dynamite_mapsdynamite@13280051@13.2.80 (040408-
211705629):5)
at com.google.maps.api.android.lib6.impl.be.run
(:com.google.android.gms.dynamite_mapsdynamite@13280051@13.2.80 (040408-
211705629):5)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
在setOnMapLoadedCallback
方法中,您需要检查 markersList 是否为空,而不是检查 poiList 是否为空。
if (!markersList.isEmpty()) {
// Adjusting Bounds
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : markersList) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
CameraUpdate mCameraUpdate =
CameraUpdateFactory.newLatLngBounds(bounds, 2);
googleMap.animateCamera(mCameraUpdate);
}
从错误来看,您的markersList
为空。它至少需要包含一个点才能LatLngBounds.Builder
正常工作。