在我按选择键时,在此行上出现错误
LatLngBounds bounds=builder.build();
尝试了许多方法,例如使用浏览器API密钥而不是google maps api密钥
private void getDirection() {
currentPosition= new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude());
String requestAPI=null;
try{
requestAPI="https://maps.googleapis.com/maps/api/directions/json?"+"mode=driving&"+
"transit_routing_preference=less_driving&"+"origin="+currentPosition.latitude+","+
currentPosition.longitude+"&"+"destination="+destination+"&"+"key="+getResources().getString(R.string.google_map_API_key);
Log.d(TAG, "getDirection: requestAPI");
mService.getPath(requestAPI)
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject= new JSONObject(response.body().toString());
JSONArray jsonArray=jsonObject.getJSONArray("routes");
for(int i=0;i<jsonArray.length();i++){
JSONObject route=jsonArray.getJSONObject(i);
JSONObject poly=route.getJSONObject("overview_polyline");
String polyline=poly.getString("points");
polyLineList=decodePoly(polyline);
}
LatLngBounds.Builder builder= new LatLngBounds.Builder();
for(LatLng latLng:polyLineList)
builder.include(latLng);
LatLngBounds bounds=builder.build();
CameraUpdate mcameraUpdate =CameraUpdateFactory.newLatLngBounds(bounds,2);
mMap.animateCamera(mcameraUpdate);
polylineOptions=new PolylineOptions();
polylineOptions.color(Color.GRAY);
polylineOptions.width(5);
polylineOptions.startCap(new SquareCap());
polylineOptions.endCap(new SquareCap());
polylineOptions.jointType(JointType.ROUND);
polylineOptions.addAll(polyLineList);
greyPolyline=mMap.addPolyline(polylineOptions);
blackpolylineOptions=new PolylineOptions();
blackpolylineOptions.color(Color.BLACK);
blackpolylineOptions.width(5);
blackpolylineOptions.startCap(new SquareCap());
blackpolylineOptions.endCap(new SquareCap());
blackpolylineOptions.jointType(JointType.ROUND);
blackPolyline=mMap.addPolyline(blackpolylineOptions);
mMap.addMarker(new MarkerOptions().position(polyLineList.get(polyLineList.size()-1))
.title("Pickup Location"));
//Animation
ValueAnimator polyLineAnimator=ValueAnimator.ofInt(0,100);
polyLineAnimator.setDuration(2000);
polyLineAnimator.setInterpolator(new LinearInterpolator());
polyLineAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
List<LatLng> points=greyPolyline.getPoints();
int percentvalue= (int) valueAnimator.getAnimatedValue();
int size=points.size();
int newpoints= (int) (size*(percentvalue/100.0f));
List<LatLng> p=points.subList(0,newpoints);
blackPolyline.setPoints(p);
}
});
polyLineAnimator.start();
carMarker=mMap.addMarker(new MarkerOptions().position(currentPosition)
.flat(true)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));
handler=new Handler();
index=-1;
next=1;
handler.postDelayed(drawPathRunnable,3000);
} catch (JSONException e) {
e.printStackTrace();
}
}
错误:
java.lang.IllegalStateException: no included points
at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source)
at com.google.android.gms.maps.model.LatLngBounds$Builder.build(Unknown Source)
at com.example.annapurna.rma.ui.MapsActivity$3.onResponse(MapsActivity.java:267)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
03-30 18:54:01.903 30134-30134/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.annapurna.rma, PID: 30134
java.lang.IllegalStateException: no included points
at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source)
at com.google.android.gms.maps.model.LatLngBounds$Builder.build(Unknown Source)
at com.example.annapurna.rma.ui.MapsActivity$3.onResponse(MapsActivity.java:267)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:0)
// checking if polylineList is Empty or not
if (!polyLineList.isEmpty()) {
// Adjusting Bounds
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Latlng latLng : polyLineList) {
builder.include(latLng);
}
LatLngBounds bounds = builder.build();
CameraUpdate mCameraUpdate =
CameraUpdateFactory.newLatLngBounds(bounds, 2);
mMap.animateCamera(mCameraUpdate);
}