Google 的 DirectionsApi 似乎在 Kotlin 中不起作用
Google’s DirectionsApi doesnt appear to work in Kotlin
我正在尝试实施 DirectionsApi 以使用 Kotlin 在我的 Android 应用中的 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 class 的 Google 地图库的一些问题有关,但由于我使用的是 kotlin,因此无法访问所述 class?这个问题有解决办法吗?
我现在自己想出了一个解决方案。这不是最好的解决方法,但可以解决手头的问题。写在这里是为了帮助其他人寻找相同问题的解决方案。
切换 gradle 依赖项:
implementation 'com.google.maps:google-maps-services:0.9.0'
至
implementation 'com.google.maps:google-maps-services:0.2.11'
它应该会修复它。
我正在尝试实施 DirectionsApi 以使用 Kotlin 在我的 Android 应用中的 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 class 的 Google 地图库的一些问题有关,但由于我使用的是 kotlin,因此无法访问所述 class?这个问题有解决办法吗?
我现在自己想出了一个解决方案。这不是最好的解决方法,但可以解决手头的问题。写在这里是为了帮助其他人寻找相同问题的解决方案。
切换 gradle 依赖项:
implementation 'com.google.maps:google-maps-services:0.9.0'
至
implementation 'com.google.maps:google-maps-services:0.2.11'
它应该会修复它。