Cos*_*tin 2 java android google-maps
我正在尝试将折线添加到当前位置和动态添加到地图的标记之间的地图。它应该工作的方式是,当InfoWindow
按下标记时,会打开一个对话框,询问您是否要前往该点,当按下“是”选项时,它应该将折线添加到该点。
单击信息窗口时:
\n\npublic void onInfoWindowClick(final Marker marker1) {\n final AlertDialog.Builder builder = new AlertDialog.Builder(this);\n builder.setMessage("Dori\xc8\x9bi s\xc4\x83 merge\xc8\x9bi la aceast\xc4\x83 alert\xc4\x83?")\n .setCancelable(true)\n .setPositiveButton("Da", new DialogInterface.OnClickListener() {\n public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {\n ScoateMarkerRute();\n markerSelectat=marker1;\n layout_confirmare.setVisibility(GONE);\n layout_validare.setVisibility(GONE);\n calculateDirections(marker1);\n dialog.dismiss();\n }\n })\n .setNegativeButton("Nu", new DialogInterface.OnClickListener() {\n public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {\n dialog.cancel();\n }\n });\n final AlertDialog alert = builder.create();\n alert.show();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n计算方向();:
\n\nprivate void calculateDirections(Marker marker){\n Log.d(TAG, "calculateDirections: calculating directions.");\n\n com.google.maps.model.LatLng destinatie = new com.google.maps.model.LatLng(\n marker.getPosition().latitude,\n marker.getPosition().longitude\n );\n DirectionsApiRequest directions = new DirectionsApiRequest(mGeoApiContext);\n\n directions.alternatives(true);\n directions.origin(\n new com.google.maps.model.LatLng(\n locatie_user.latitude,\n locatie_user.longitude\n )\n );\n Log.d(TAG, "calculateDirections: destination: " + destinatie.toString());\n directions.destination(destinatie).setCallback(new PendingResult.Callback<DirectionsResult>() {\n @Override\n public void onResult(DirectionsResult result) {\n Log.d(TAG, "calculateDirections: routes: " + result.routes[0].toString());\n Log.d(TAG, "calculateDirections: duration: " + result.routes[0].legs[0].duration);\n Log.d(TAG, "calculateDirections: distance: " + result.routes[0].legs[0].distance.inMeters);\n Log.d(TAG, "calculateDirections: geocodedWayPoints: " + result.geocodedWaypoints[0].toString());\n addPolylinesToMap(result);\n }\n\n @Override\n public void onFailure(Throwable e) {\n Log.e(TAG, "calculateDirections: Failed to get directions: " + e.getMessage() );\n\n }\n });\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n添加多段线到地图:
\n\nprivate void addPolylinesToMap(final DirectionsResult result){\n new Handler(Looper.getMainLooper()).post(new Runnable() {\n @Override\n public void run() {\n Log.d(TAG, "run: result routes: " + result.routes.length);\n if(mPolylinesData.size()>0){\n for(PolylineData polylineData: mPolylinesData){\n polylineData.getPolyline().remove();\n }\n mPolylinesData.clear();\n mPolylinesData= new ArrayList<>();\n }\n for(DirectionsRoute route: result.routes){\n Log.d(TAG, "run: leg: " + route.legs[0].toString());\n List<com.google.maps.model.LatLng> decodedPath = PolylineEncoding.decode(route.overviewPolyline.getEncodedPath());\n\n List<LatLng> newDecodedPath = new ArrayList<>();\n\n // This loops through all the LatLng coordinates of ONE polyline.\n for(com.google.maps.model.LatLng latLng: decodedPath){\n Log.d(TAG, "run: latlng: " + latLng.toString());\n\n newDecodedPath.add(new LatLng(\n latLng.lat,\n latLng.lng\n ));\n }\n Polyline polyline = mMap.addPolyline(new PolylineOptions().addAll(newDecodedPath));\n polyline.setColor(ContextCompat.getColor(Harta.this, R.color.scope_systems_gri));\n polyline.setWidth(25);\n polyline.setClickable(true);\n polyline.setTag("A");\n mPolylinesData.add(new PolylineData(polyline, route.legs[0]));\n onPolylineClick(polyline);\n zoomRoute(polyline.getPoints());\n markerSelectat.setVisible(false);\n }\n }\n });\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n当我实现这种添加折线的方法时,我使用了一张充满标记的地图,并且确实有效。当我更改并仅添加 1 个标记时,应用程序不断崩溃并出现错误:
\n\n> > java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/ZonedDateTime;\n> at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241)\n> at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207)\n> at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)\n> at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)\n> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)\n> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)\n> at java.lang.Thread.run(Thread.java:761)\n> Caused by: java.lang.ClassNotFoundException: Didn\'t find class "java.time.ZonedDateTime" on path: DexPathList[[zip file\n> "/data/app/com.example.aplicatieprimarie-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.aplicatieprimarie-2/lib/x86,\n> /system/lib, /vendor/lib]]\n> at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)\n> at java.lang.ClassLoader.loadClass(ClassLoader.java:380)\n> at java.lang.ClassLoader.loadClass(ClassLoader.java:312)\n> at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241)\xc2\xa0\n> at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207)\xc2\xa0\n> at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)\xc2\xa0\n> at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)\xc2\xa0\n> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)\xc2\xa0\n> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)\xc2\xa0\n> at java.lang.Thread.run(Thread.java:761)\n
Run Code Online (Sandbox Code Playgroud)\n\n\xc2\xa0\n编辑:添加了 build.gradle
\n\n我已更新: \n-\'com.google.android.libraries.places:places-compat:2.1.0\'
到\'com.google.android.libraries.places:places-compat:2.1.1\'
\n 现在同步后,当我尝试运行时出现以下错误:
\n\n\n原因:com.android.tools.r8.utils.AbortException:错误:null,无法在单个 dex 文件中容纳请求的类(# 方法:66003 > 65536)
\n
构建.gradle(应用程序):
\n\napply plugin: \'com.android.application\'\n\nandroid {\n compileSdkVersion 29\n buildToolsVersion "29.0.2"\n defaultConfig {\n applicationId "com.example.aplicatieprimarie"\n minSdkVersion 16\n targetSdkVersion 29\n versionCode 1\n versionName "1.0"\n testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"\n }\n buildTypes {\n release {\n minifyEnabled false\n proguardFiles getDefaultProguardFile(\'proguard-android-optimize.txt\'), \'proguard-rules.pro\'\n }\n }\n compileOptions {\n sourceCompatibility JavaVersion.VERSION_1_8\n targetCompatibility JavaVersion.VERSION_1_8\n }\n\n}\n\ndependencies {\n implementation fileTree(dir: \'libs\', include: [\'*.jar\'])\n implementation \'androidx.appcompat:appcompat:1.1.0\'\n implementation \'androidx.constraintlayout:constraintlayout:1.1.3\'\n implementation \'com.google.android.gms:play-services-maps:17.0.0\'\n testImplementation \'junit:junit:4.12\'\n androidTestImplementation \'androidx.test.ext:junit:1.1.1\'\n androidTestImplementation \'androidx.test.espresso:espresso-core:3.2.0\'\n implementation \'com.facebook.android:facebook-android-sdk:5.9.0\'\n implementation "com.google.android.gms:play-services-location:17.0.0"\n implementation \'de.hdodenhof:circleimageview:3.0.1\'\n implementation \'com.github.bumptech.glide:glide:4.10.0\'\n annotationProcessor \'com.github.bumptech.glide:compiler:4.10.0\'\n implementation \'com.google.android.gms:play-services-auth:17.0.0\'\n implementation \'com.google.android.material:material:1.1.0\'\n implementation \'com.google.android.material:material:1.1.0\'\n implementation \'androidx.constraintlayout:constraintlayout:1.1.3\'\n implementation \'androidx.navigation:navigation-fragment:2.2.1\'\n implementation \'androidx.navigation:navigation-ui:2.2.1\'\n implementation \'androidx.lifecycle:lifecycle-extensions:2.2.0\'\n androidTestImplementation \'androidx.test.ext:junit:1.1.1\'\n androidTestImplementation \'androidx.test.espresso:espresso-core:3.2.0\'\n implementation \'com.google.android.libraries.places:places-compat:2.2.0\'\n implementation \'androidx.biometric:biometric:1.0.1\'\n implementation \'com.google.maps:google-maps-services:0.10.2\'\n implementation \'org.slf4j:slf4j-simple:1.7.25\'\n}\n
Run Code Online (Sandbox Code Playgroud)\n
根据Google 地图服务的文档,这不是为 Android 设计的。
Google 地图服务的 Java 客户端设计用于服务器应用程序。由于可能会丢失 API 密钥,因此该库不适合在 Android 应用程序内部使用。
您需要java.time
处理时区 API。作为解决方法,您可以使用以下依赖项,即java.time
包 API的 Backport
implementation group: 'com.github.seratch', name: 'java-time-backport', version: '1.0.0'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2216 次 |
最近记录: |