diff --git a/.gitignore b/.gitignore index 39fb081..63cb77a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ *.iml .gradle /local.properties -/.idea/workspace.xml -/.idea/libraries +.idea .DS_Store /build /captures diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index d3d6972..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 7f68460..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 46a6ed4..193e4f6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,10 +24,10 @@ You can define the keys for the debug and release targets in src/debug/ and src/release/. --> - + diff --git a/app/src/main/java/com/example/prabhat/locationsample/MapsActivity.kt b/app/src/main/java/com/example/prabhat/locationsample/MapsActivity.kt index 1444414..fee933e 100644 --- a/app/src/main/java/com/example/prabhat/locationsample/MapsActivity.kt +++ b/app/src/main/java/com/example/prabhat/locationsample/MapsActivity.kt @@ -2,6 +2,7 @@ package com.example.prabhat.locationsample import android.graphics.Color import android.os.Bundle +import android.os.Handler import android.util.Log import androidx.appcompat.app.AppCompatActivity import com.example.easywaylocation.draw_path.DirectionUtil @@ -11,12 +12,19 @@ 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.PolylineOptions + class MapsActivity : AppCompatActivity(), OnMapReadyCallback, DirectionUtil.DirectionCallBack { + companion object{ + val WAY_POINT_TAG = "way_point_tag" + val ARC_POINT_TAG = "arc_point_tag" + } + override fun pathFindFinish(polyLineDetails: HashMap) { for (i in polyLineDetails.keys){ - Log.v("sample",polyLineDetails[i]?.time) + Log.v("sample", polyLineDetails[i]?.time) } } @@ -34,7 +42,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, DirectionUtil.Dire override fun onMapReady(googleMap: GoogleMap) { mMap = googleMap - mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.423669, -122.090168),16F)) + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(37.423669, -122.090168), 16F)) wayPoints.add(LatLng(37.423669, -122.090168)) wayPoints.add(LatLng(37.420930, -122.085362)) val directionUtil = DirectionUtil.Builder() @@ -42,13 +50,22 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, DirectionUtil.Dire .setOrigin(LatLng(37.421481, -122.092156)) .setWayPoints(wayPoints) .setGoogleMap(mMap) - .setPathAnimation(false) - .setPolyLinePrimaryColor(Color.WHITE) - .setPolyLineWidth(8) + .setPolyLinePrimaryColor(R.color.black) + .setPolyLineWidth(5) + .setPathAnimation(true) .setCallback(this) + .setPolylineTag(WAY_POINT_TAG) .setDestination(LatLng(37.421519, -122.086809)) .build() directionUtil.drawPath() + directionUtil.drawArcDirection(LatLng(37.421481, -122.092156),LatLng(37.421519, -122.086809),0.5,ARC_POINT_TAG) + Handler().postDelayed({ directionUtil.clearPolyline(WAY_POINT_TAG) }, 3000) + + Handler().postDelayed({ directionUtil.clearPolyline(ARC_POINT_TAG) }, 6000) } + + + + } diff --git a/build.gradle b/build.gradle index 44727a2..d38aa35 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.61' + ext.kotlin_version = '1.4.32' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.6.1' + classpath 'com.android.tools.build:gradle:4.1.3' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // NOTE: Do not place your application dependencies here; they belong diff --git a/easywaylocation/build.gradle b/easywaylocation/build.gradle index 522718c..1f66f15 100644 --- a/easywaylocation/build.gradle +++ b/easywaylocation/build.gradle @@ -41,8 +41,10 @@ dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5" implementation 'com.squareup.retrofit2:retrofit:2.6.1' implementation 'com.squareup.retrofit2:converter-scalars:2.6.1' - implementation "androidx.core:core-ktx:+" + implementation "androidx.core:core-ktx:1.5.0" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'com.google.maps.android:android-maps-utils:0.6.2' + } repositories { mavenCentral() diff --git a/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/DirectionUtil.kt b/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/DirectionUtil.kt index 9110d2c..3cd5bc1 100644 --- a/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/DirectionUtil.kt +++ b/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/DirectionUtil.kt @@ -9,6 +9,7 @@ import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.model.JointType import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.PolylineOptions +import com.google.maps.android.SphericalUtil import kotlinx.coroutines.* import org.json.JSONObject import java.io.BufferedReader @@ -27,6 +28,7 @@ class DirectionUtil private constructor(builder: Builder) { private var origin: LatLng? private var destination: LatLng? private var polyLineWidth = 10 + private var mTag:String private var pathAnimation:Boolean private var polyLinePrimaryColor:Int = 0 private var polyLineSecondaryColor = 0 @@ -39,9 +41,10 @@ class DirectionUtil private constructor(builder: Builder) { private var primaryLineCompletionTime = 2000 private var animationDelay = 200 - + private var polylineMap:HashMap = HashMap() init { + this.mTag = builder.mTag this.directionCallBack = builder.directionCallBack this.destination = builder.destination this.directionKey = builder.key @@ -172,6 +175,7 @@ class DirectionUtil private constructor(builder: Builder) { mapAnimator.setCompletionTime(pathCompletionTime) mapAnimator.setPrimaryLineCompletion(primaryLineCompletionTime) mapAnimator.animateRoute(it, allPathPoints,polyLineDataBean) + polylineMap.put(mTag,mapAnimator) } } } @@ -251,6 +255,57 @@ class DirectionUtil private constructor(builder: Builder) { return "https://maps.googleapis.com/maps/api/directions/$output?$parameters" } + fun drawArcDirection(origin: LatLng, destination: LatLng, radius: Double,tag:String) { + val allPathPoints:ArrayList = ArrayList() + val d: Double = SphericalUtil.computeDistanceBetween(origin, destination) + val h: Double = SphericalUtil.computeHeading(origin, destination) + + //Midpoint position + val p: LatLng = SphericalUtil.computeOffset(origin, d * 0.5, h) + + val x = (1 - radius * radius) * d * 0.5 / (2 * radius) + val r = (1 + radius * radius) * d * 0.5 / (2 * radius) + val c: LatLng = SphericalUtil.computeOffset(p, x, h + 90.0) + + //Calculate heading between circle center and two points + val h1: Double = SphericalUtil.computeHeading(c, origin) + val h2: Double = SphericalUtil.computeHeading(c, destination) + + //Calculate positions of points on circle border and add them to polyline options + val numpoints = 100 + val step = (h2 - h1) / numpoints + for (i in 0 until numpoints) { + val pi: LatLng = SphericalUtil.computeOffset(c, r, h1 + i * step) + allPathPoints.add(pi) + } + + mMap?.let { + val mapAnimator = MapAnimator() + mapAnimator.setColorFillCompletion(pathColorFillAnimationTime) + mapAnimator.setDelayTime(animationDelay) + mapAnimator.setPrimaryLineColor(polyLinePrimaryColor) + mapAnimator.setSecondaryLineColor(polyLineSecondaryColor) + mapAnimator.setCompletionTime(pathCompletionTime) + mapAnimator.setPrimaryLineCompletion(primaryLineCompletionTime) + mapAnimator.animateRoute(it, allPathPoints,polyLineDataBean) + polylineMap.put(tag,mapAnimator) + } + + } + + fun clearPolyline(mTag: String){ + if (!polylineMap.containsKey(mTag)){ + throw java.lang.Exception("No Polyline Tag Found") + } + polylineMap.get(mTag)?.getBck()?.let { + polylineMap.get(mTag)?.getFor()?.remove() + it.remove() + }?:run{ + throw java.lang.Exception("Please initiate polyline before calling this.") + } + + } + interface DirectionCallBack { fun pathFindFinish(polyLineDetails: HashMap) @@ -261,6 +316,8 @@ class DirectionUtil private constructor(builder: Builder) { private set var mMap: GoogleMap? = null private set + var mTag: String = "" + private set var key: String? = null private set var wayPoints = ArrayList() @@ -316,6 +373,11 @@ class DirectionUtil private constructor(builder: Builder) { return this } + fun setPolylineTag(mTag: String): Builder { + this.mTag = mTag + return this + } + fun setPolyLineWidth(polyLineWidth: Int): Builder { this.polyLineWidth = polyLineWidth return this @@ -346,4 +408,6 @@ class DirectionUtil private constructor(builder: Builder) { } } + + } diff --git a/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/MapAnimator.kt b/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/MapAnimator.kt index 0c94471..46cb6b4 100644 --- a/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/MapAnimator.kt +++ b/easywaylocation/src/main/java/com/example/easywaylocation/draw_path/MapAnimator.kt @@ -9,9 +9,10 @@ import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.Polyline import com.google.android.gms.maps.model.PolylineOptions +import java.lang.Exception -class MapAnimator { +internal class MapAnimator { private var backgroundPolyline: Polyline? = null @@ -83,11 +84,11 @@ class MapAnimator { if (backgroundPolyline != null) backgroundPolyline!!.remove() - val optionsBackground = PolylineOptions().add(routes[0]).color(backgroundColor!!).width(8f) + val optionsBackground = PolylineOptions().add(routes[0]).color(backgroundColor!!).width(8f)?.geodesic(false) backgroundPolyline = googleMap.addPolyline(optionsBackground) - optionsForeground = PolylineOptions().add(routes[0]).color(foregroundColor!!).width(8f) + optionsForeground = PolylineOptions().add(routes[0]).color(foregroundColor!!).width(8f)?.geodesic(false) foregroundPolyline = googleMap.addPolyline(optionsForeground) // foregroundPolyline?.tag = getTag() // polyLineDetails[foregroundPolyline?.tag as String] = polyLineDataBean @@ -208,5 +209,21 @@ class MapAnimator { foregroundPoints.add(endLatLng) foregroundPolyline!!.points = foregroundPoints } + + fun getFor():Polyline{ + foregroundPolyline?.let { + return it; + }?:run{ + throw Exception("Please initiate polyline before calling this.") + } + } + + fun getBck():Polyline{ + backgroundPolyline?.let { + return it; + }?:run{ + throw Exception("Please initiate polyline before calling this.") + } + } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 675c96b..e9df285 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Mar 13 23:51:57 IST 2020 +#Wed Dec 08 01:01:22 IST 2021 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip