Skip to content

Commit

Permalink
Merge pull request #20 from prabhat1707/feature/arc_polyline
Browse files Browse the repository at this point in the history
Feature/arc polyline
  • Loading branch information
prabhat1707 authored Dec 8, 2021
2 parents 899ec89 + a021c16 commit 422c024
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 51 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.idea
.DS_Store
/build
/captures
Expand Down
17 changes: 0 additions & 17 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<activity
android:name=".MapsActivity"
android:name=".MainActivity"
android:label="@string/title_activity_maps"></activity>

<activity android:name=".MainActivity">
<activity android:name=".MapsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, PolyLineDataBean>) {
for (i in polyLineDetails.keys){
Log.v("sample",polyLineDetails[i]?.time)
Log.v("sample", polyLineDetails[i]?.time)
}
}

Expand All @@ -34,21 +42,30 @@ 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()
.setDirectionKey("xyz")
.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)
}




}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion easywaylocation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -39,9 +41,10 @@ class DirectionUtil private constructor(builder: Builder) {
private var primaryLineCompletionTime = 2000

private var animationDelay = 200

private var polylineMap:HashMap<String,MapAnimator> = HashMap()

init {
this.mTag = builder.mTag
this.directionCallBack = builder.directionCallBack
this.destination = builder.destination
this.directionKey = builder.key
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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<LatLng> = 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<String, PolyLineDataBean>)
Expand All @@ -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<LatLng>()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -346,4 +408,6 @@ class DirectionUtil private constructor(builder: Builder) {
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.")
}
}
}

4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 422c024

Please sign in to comment.