Adding polylines to map error Failed resolution of: Ljava/time/ZonedDateTime

Costin :

I am trying to add polylines to a map between the current location and a marker added dynamically to the map. The way it should work it's when the InfoWindow from the marker it's pressed, a dialog asking if you wanna travel to that point opens, and when the "Yes" option is pressed, it should add the polyline to that point.

OnInfoWindowClick :

public void onInfoWindowClick(final Marker marker1) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Doriți să mergeți la această alertă?")
            .setCancelable(true)
            .setPositiveButton("Da", new DialogInterface.OnClickListener() {
                public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                    ScoateMarkerRute();
                    markerSelectat=marker1;
                    layout_confirmare.setVisibility(GONE);
                    layout_validare.setVisibility(GONE);
                    calculateDirections(marker1);
                    dialog.dismiss();
                }
            })
            .setNegativeButton("Nu", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                    dialog.cancel();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();
}

calculateDirections(); :

private void calculateDirections(Marker marker){
    Log.d(TAG, "calculateDirections: calculating directions.");

    com.google.maps.model.LatLng destinatie = new com.google.maps.model.LatLng(
            marker.getPosition().latitude,
            marker.getPosition().longitude
    );
    DirectionsApiRequest directions = new DirectionsApiRequest(mGeoApiContext);

    directions.alternatives(true);
    directions.origin(
            new com.google.maps.model.LatLng(
                    locatie_user.latitude,
                    locatie_user.longitude
            )
    );
    Log.d(TAG, "calculateDirections: destination: " + destinatie.toString());
    directions.destination(destinatie).setCallback(new PendingResult.Callback<DirectionsResult>() {
        @Override
        public void onResult(DirectionsResult result) {
            Log.d(TAG, "calculateDirections: routes: " + result.routes[0].toString());
            Log.d(TAG, "calculateDirections: duration: " + result.routes[0].legs[0].duration);
            Log.d(TAG, "calculateDirections: distance: " + result.routes[0].legs[0].distance.inMeters);
            Log.d(TAG, "calculateDirections: geocodedWayPoints: " + result.geocodedWaypoints[0].toString());
            addPolylinesToMap(result);
        }

        @Override
        public void onFailure(Throwable e) {
            Log.e(TAG, "calculateDirections: Failed to get directions: " + e.getMessage() );

        }
    });
}

addPolylinesToMap :

private void addPolylinesToMap(final DirectionsResult result){
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "run: result routes: " + result.routes.length);
            if(mPolylinesData.size()>0){
                for(PolylineData polylineData: mPolylinesData){
                    polylineData.getPolyline().remove();
                }
                mPolylinesData.clear();
                mPolylinesData= new ArrayList<>();
            }
            for(DirectionsRoute route: result.routes){
                Log.d(TAG, "run: leg: " + route.legs[0].toString());
                List<com.google.maps.model.LatLng> decodedPath = PolylineEncoding.decode(route.overviewPolyline.getEncodedPath());

                List<LatLng> newDecodedPath = new ArrayList<>();

                // This loops through all the LatLng coordinates of ONE polyline.
                for(com.google.maps.model.LatLng latLng: decodedPath){
                Log.d(TAG, "run: latlng: " + latLng.toString());

                newDecodedPath.add(new LatLng(
                            latLng.lat,
                            latLng.lng
                    ));
                }
                Polyline polyline = mMap.addPolyline(new PolylineOptions().addAll(newDecodedPath));
                polyline.setColor(ContextCompat.getColor(Harta.this, R.color.scope_systems_gri));
                polyline.setWidth(25);
                polyline.setClickable(true);
                polyline.setTag("A");
                mPolylinesData.add(new PolylineData(polyline, route.legs[0]));
                onPolylineClick(polyline);
                zoomRoute(polyline.getPoints());
                markerSelectat.setVisible(false);
            }
        }
    });
}

When I implemented this method of adding polylines, I used a map full of markers and actually worked. When I changed and added only 1 marker, the app kept crashing with the error :

> > java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/ZonedDateTime;
>         at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241)
>         at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207)
>         at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
>         at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
>         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
>         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
>         at java.lang.Thread.run(Thread.java:761)
>      Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.ZonedDateTime" on path: DexPathList[[zip file
> "/data/app/com.example.aplicatieprimarie-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.aplicatieprimarie-2/lib/x86,
> /system/lib, /vendor/lib]]
>         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
>         at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
>         at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
>         at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241) 
>         at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207) 
>         at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174) 
>         at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
>         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
>         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
>         at java.lang.Thread.run(Thread.java:761)

  EDIT: added build.gradle

I have updated: -'com.google.android.libraries.places:places-compat:2.1.0' to 'com.google.android.libraries.places:places-compat:2.1.1' and now after syncing, when I try to run I get the following error:

Caused by: com.android.tools.r8.utils.AbortException: Error: null, Cannot fit requested classes in a single dex file (# methods: 66003 > 65536)

build.gradle(app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.aplicatieprimarie"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.facebook.android:facebook-android-sdk:5.9.0'
    implementation "com.google.android.gms:play-services-location:17.0.0"
    implementation 'de.hdodenhof:circleimageview:3.0.1'
    implementation 'com.github.bumptech.glide:glide:4.10.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.1'
    implementation 'androidx.navigation:navigation-ui:2.2.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.libraries.places:places-compat:2.2.0'
    implementation 'androidx.biometric:biometric:1.0.1'
    implementation 'com.google.maps:google-maps-services:0.10.2'
    implementation 'org.slf4j:slf4j-simple:1.7.25'
}
Md. Asaduzzaman :

According documentation of Google Maps Services, this is not designed for android.

The Java Client for Google Maps Services is designed for use in server applications. This library is not intended for use inside of an Android app, due to the potential for loss of API keys.

Requirements

  • Java 1.8 or later.
  • A Google Maps API key.

You need java.time to handle Time Zone API. As a workaround you can use below dependency which is Backport of java.time package API

implementation group: 'com.github.seratch', name: 'java-time-backport', version: '1.0.0'

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=420516&siteId=1