簡體   English   中英

獲取方向並獲取路徑android studio

[英]get Direction and get path android studio

我試圖從自動完成形式中選擇一個地方,並為其繪制了一條路徑,當我從自動完成中選擇一個地方時,應用程序崩潰了。 請看

---------崩潰開始

E / AndroidRuntime:致命例外:主進程:com.saoutimohamed.tewsila,PID:5924 java.lang.IllegalStateException:com.google.android.gms.common.internal.Preconditions.checkState中沒有包含點(未知來源:8) com.saoutimohamed.tewsila.WelcomeDriver $ 4.onResponse(WelcomeDriver.java:271)上com.google.android.gms.maps.model.LatLngBounds $ Builder.build(未知源:10)上​​retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ .run(ExecutorCallAdapterFactory.java:70)在android.os.Handler.handleCallback(Handler.java:789)在android.os.Handler.dispatchMessage(Handler.java:98)在android.os.Looper.loop(Looper。 android.app.ActivityThread.main(ActivityThread.java:6938)處的java.164)com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java.lang.reflect.Method.invoke(本機方法)處java:327)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)應用程序終止。

logcat和高個子我代碼有什么問題

這是代碼

 private void getDirection() { String requestApi; try { requestApi = "https://maps.googleapis.com/maps/api/directions/json?" + "mode=driving&" + "transit_routing_preference=less_driving&" + "origin=" + Common.mLastLocation.getLatitude() + "," + Common.mLastLocation.getLongitude() + "&" + "destination=" + lat+","+lng + "&" + "key=" + getResources().getString(R.string.google_direction_api); Log.d("SAOUTI", 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, 5); 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")); 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.mipmap.tewsila_car))); handler = new Handler(); } catch (JSONException e) { e.printStackTrace(); index = -1; next = 1; handler.postDelayed(drawPathRunnable, 3000); } } @Override public void onFailure(Call<String> call, Throwable t) { Toast.makeText(WelcomeDriver.this, "" + t.getMessage(), Toast.LENGTH_SHORT).show(); } }); } catch (Exception e) { e.printStackTrace(); } } 
在logcat中沒有響應的行是(LatLngBounds bounds = builder.build();)

我遇到了同樣的問題,並從requestApi中刪除了“ + getResources()。getString(R.string.google_direction_api)”部分。 Perfectlr為我工作。 但我不知道原因。

requestApi = "https://maps.googleapis.com/maps/api/directions/json?" +
                "mode=driving&" +
                "transit_routing_preference=less_driving&" +
                "origin=" + Common.mLastLocation.getLatitude() + "," + Common.mLastLocation.getLongitude() + "&" +
                "destination=" + lat+","+lng + "&" +
                "key=";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM