Google的DirectionsApi在Kotlin中似乎不起作用

时间:2018-10-01 12:05:27

标签: java android google-maps kotlin google-maps-android-api-2

我正在尝试使用DirectionsApi在我的Android应用中使用Kotlin在Google地图上绘制折线。我使用以下方法添加了所需的库:

implementation 'com.google.maps:google-maps-services:0.9.0'

然后在我的MapFragment中包含以下功能:

private fun getGeoContext() : GeoApiContext {
    val geoApiContext = GeoApiContext.Builder()
            .apiKey(getString(R.string.directions_apikey))
            .build()
    return geoApiContext
}

最后,在我的onMapReady中,我尝试使用以下方法获取路线结果:

val origin = "Hotel Hillview Munnar, State Highway 16, Moolakadai, Munnar, Kerala, India"
val dest = "Echo Point, Kannan Devan Hills, Kerala, India"

val apiRequest = DirectionsApi.newRequest(getGeoContext())
        apiRequest.origin(origin)
        apiRequest.destination(dest)
        apiRequest.mode(TravelMode.DRIVING)
        apiRequest.setCallback(object : com.google.maps.PendingResult.Callback<DirectionsResult> {
            override fun onResult(result:DirectionsResult) {
                Timber.i("Call Success")
                val routes = result.routes
            }
            override fun onFailure(e:Throwable) {
            }
        })

但是它一直在失败。我检查了Logcat日志,发现这是错误:

10-01 17:22:41.248 4112-4193/com.acme.internal E/AndroidRuntime: FATAL EXCEPTION: Rate Limited Dispatcher
    Process: com.acme.internal, PID: 4112
    java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDateTime;
        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:153)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.LocalDateTime" on path: DexPathList[[zip file "/data/app/com.acme.internal-1/base.apk"],nativeLibraryDirectories=[/data/app/com.acme.internal-1/lib/arm64, /vendor/lib64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        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:153) 
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 
      Suppressed: java.lang.ClassNotFoundException: java.time.LocalDateTime
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 8 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
10-01 17:22:41.248 4112-4193/com.acme.internal E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()

我猜想这与使用LocalDateTime类的Google Maps库有关,但是由于我使用的是Kotlin,它无法访问上述类?这个问题有解决方法吗?

1 个答案:

答案 0 :(得分:0)

我现在自己想出了一个解决方案。这不是最佳解决方案,但可以解决当前的问题。在此处写下它,这样可能会帮助其他人寻求同一问题的解决方案。

从以下位置切换gradle依赖项:

implementation 'com.google.maps:google-maps-services:0.9.0'

implementation 'com.google.maps:google-maps-services:0.2.11'

它应该修复它。