0

I am trying to display the distance between two points as a polyline on the google maps in my app. For some reason this line of code keeps giving me the same exception which causes my app to crash at run time after my main activity is displayed & then when I open this activity by pressing a button. Here is the line of code:

GeoApiContext context = new GeoApiContext.Builder()
                .apiKey("apikey")
                .build();

Here is the exception I recieve:

java.lang.ExceptionInInitializerError
    at okhttp3.internal.platform.Platform.get(Platform.java:85)
    at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229)
    at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1015)
    at com.google.maps.OkHttpRequestHandler$Builder.build(OkHttpRequestHandler.java:201)
    at com.google.maps.GeoApiContext$Builder.build(GeoApiContext.java:625)
    at com.project.prayerpal.ClosestDestination.onMapReady(ClosestDestination.java:89)
    at com.google.android.gms.maps.zzat.zzb(com.google.android.gms:play-services-maps@@18.1.0:1)
    at com.google.android.gms.maps.internal.zzar.zza(com.google.android.gms:play-services-maps@@18.1.0:6)
    at com.google.android.gms.internal.maps.zzb.onTransact(com.google.android.gms:play-services-maps@@18.1.0:3)
    at android.os.Binder.transact(Binder.java:1084)
    at fe.c(:com.google.android.gms.dynamite_mapsdynamite@231254044@23.12.54 (190400-0):2)
    at com.google.maps.api.android.lib6.impl.bg.run(:com.google.android.gms.dynamite_mapsdynamite@231254044@23.12.54 (190400-0):1)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:233)
    at android.os.Looper.loop(Looper.java:344)
    at android.app.ActivityThread.main(ActivityThread.java:8212)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
 Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 31
    at okhttp3.internal.platform.AndroidPlatform.buildIfSupported(AndroidPlatform.java:238)
    at okhttp3.internal.platform.Platform.findPlatform(Platform.java:202)
    at okhttp3.internal.platform.Platform.<clinit>(Platform.java:79)
    at okhttp3.internal.platform.Platform.get(Platform.java:85) 
    at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263) 
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229) 
    at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1015) 
    at com.google.maps.OkHttpRequestHandler$Builder.build(OkHttpRequestHandler.java:201) 
    at com.google.maps.GeoApiContext$Builder.build(GeoApiContext.java:625) 
    at com.project.prayerpal.ClosestDestination.onMapReady(ClosestDestination.java:89) 
    at com.google.android.gms.maps.zzat.zzb(com.google.android.gms:play-services-maps@@18.1.0:1) 
    at com.google.android.gms.maps.internal.zzar.zza(com.google.android.gms:play-services-maps@@18.1.0:6) 
    at com.google.android.gms.internal.maps.zzb.onTransact(com.google.android.gms:play-services-maps@@18.1.0:3) 
    at android.os.Binder.transact(Binder.java:1084) 
    at fe.c(:com.google.android.gms.dynamite_mapsdynamite@231254044@23.12.54 (190400-0):2) 
    at com.google.maps.api.android.lib6.impl.bg.run(:com.google.android.gms.dynamite_mapsdynamite@231254044@23.12.54 (190400-0):1) 
    at android.os.Handler.handleCallback(Handler.java:938) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loopOnce(Looper.java:233) 
    at android.os.Looper.loop(Looper.java:344) 
    at android.app.ActivityThread.main(ActivityThread.java:8212) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034) 

I replace 'apikey' with an actual api key from my google developer console. I've researched & done many things such as making sure this exception is not caused by a restriction on my apikey, used a second apikey, check all my dependancies to make sure they're correct & up to date, made sure everything is correctly typed in my code, made sure my billing account is properly configured, made sure I am using the correct api key for this project, made sure my compileSdkVersion & targetSdkVersion are higher than 21, added all the necessary permissions, cleaned my project & restarted/invalidated cache but still nothing seems to work.

The apikey I am using for the maps directions api is the same key I am using elsewhere in the same project for the maps places api & it seems to be working there.

Here is my code for this activity:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import com.google.maps.DirectionsApi;
import com.google.maps.DirectionsApiRequest;
import com.google.maps.GeoApiContext;
import com.google.maps.errors.ApiException;
import com.google.maps.model.DirectionsResult;
import com.google.maps.model.DirectionsRoute;
import com.google.maps.model.TravelMode;
import com.google.maps.android.PolyUtil;

import java.io.IOException;
import java.util.List;

public class ClosestDestination extends AppCompatActivity implements OnMapReadyCallback{

LatLng currentLocation;
LatLng destinationLocation;
String destinationName;
double originLat;
double originLng;
double destinationLat;
double destinationLng;
SupportMapFragment mapFragment;
MarkerOptions starterMarkerOptions;
MarkerOptions endMarkerOptions;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_closest_mosque);

    Intent intent = getIntent();
    originLat = intent.getDoubleExtra("originLat", 0.0);
    originLng = intent.getDoubleExtra("originLng", 0.0);

    currentLocation = new LatLng(originLat, originLng);

    destinationLat = Double.parseDouble(MainActivity.placesData.getMapList().get(0).get("destinationLat"));
    destinationLng = Double.parseDouble(MainActivity.placesData.getMapList().get(0).get("destinationLng"));
    destinationName = MainActivity.placesData.getMapList().get(0).get("destinationName");

    destinationLocation = new LatLng(destinationLat, destinationLng);

    starterMarkerOptions = new MarkerOptions().position(currentLocation).title("current location");
    endMarkerOptions = new MarkerOptions().position(destinationLocation).title("destination");

    mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    mapFragment.getMapAsync(ClosestDestination.this);
}

@Override
public void onMapReady(@NonNull GoogleMap googleMap) {

    try {

        GeoApiContext context = new GeoApiContext.Builder()
                .apiKey("apikey")
                .build();

        DirectionsApiRequest directionsApiRequest = DirectionsApi.newRequest(context)
                .origin(currentLocation.toString())
                .destination(destinationLocation.toString())
                .mode(TravelMode.WALKING);

        DirectionsResult directionsResult = null;

        try {
            directionsResult = directionsApiRequest.await();

            if (directionsResult.routes.length > 0) {

                DirectionsRoute route = directionsResult.routes[0];

                String polyLine = route.overviewPolyline.getEncodedPath();

                List<LatLng> points = PolyUtil.decode(polyLine);

                for (LatLng point : points) {

                    googleMap.addMarker(new MarkerOptions().position(point));
                }

            }

        } catch (ApiException | InterruptedException | IOException e) {

            e.printStackTrace();
        }

        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        googleMap.addMarker(starterMarkerOptions);
        googleMap.addMarker(endMarkerOptions);
        googleMap.moveCamera(CameraUpdateFactory.zoomTo(14));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));

    }catch (Exception e){

        e.printStackTrace();
    }

}

}

Here is also the XML code:

<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
kinghabz 14
  • 7
  • 1
  • 2

0 Answers0