places = (PlaceAutocompleteFragment)getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
places.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
if (location_switch.isChecked()) {
destination = place.getAddress().toString();
destination = destination.replace(" ", "+");
Log.d("Welcome", destination);
getDirection();
}else
{
Toast.makeText(Welcome.this, "Please ONLINE",Toast.LENGTH_SHORT)
.show();
}
}
@Override
public void onError(Status status) {
Toast.makeText(Welcome.this,""+status.toString(),Toast.LENGTH_SHORT).show();
}
});
drivers = FirebaseDatabase.getInstance().getReference("Drivers");
geoFire = new GeoFire(drivers);
setUpLocation();
}
private void getDirection() {
currentPosition = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
@NonNull
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_direction_api);
Log.d("Welcome", 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());
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();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(Welcome.this, " "+t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}catch (Exception e)
{
e.printStackTrace();
}
}
错误日志:
D/i-way: https://maps.googleapis.com/maps/api/directions/json?mode=driving&transit_routing_preference=less_driving&origin=37.4219983,-122.084&destinationDubai+-+United+Arab+Emirates&key=AIzaSyDk9oik-iVXrADcSniX-LE2PRX7FAeTEV0
V/FA: Activity resumed, time: 144867953
D/EGL_emulation: eglMakeCurrent: 0xa3ba91c0: ver 1 0
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: app.asliborneo.dev.iwaybiz, PID: 2444
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:156)
at org.json.JSONObject.<init>(JSONObject.java:173)
at app.asliborneo.dev.iwaybiz.Welcome$4.onResponse(Welcome.java:292)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I/CrashlyticsCore: Crashlytics report upload complete: 5BB6F27300EA-0001-098C-D8EF44217386
答案 0 :(得分:0)
在您的查询响应中,“ routes”作为null出现,这就是为什么获取null jsonArray且jsonArray.length函数失败的原因。
请检查您的URL,好像没有正确提供目的地。它应该是目的地** = **迪拜
答案 1 :(得分:0)
似乎所选路径或传输的路由数据为空。尝试以下代码。
if(jsonArray != null){
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);
}
}else {
// Route not found
}