我正在尝试使用 Kotlin 在我的 Android 应用程序中实现 DirectionsApi 以在 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) {
}
})
FFIVE
相关分类