Skip to content

Commit

Permalink
fix major bug and add more features
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhat1707 committed Dec 24, 2021
1 parent a021c16 commit 9f20ba7
Show file tree
Hide file tree
Showing 16 changed files with 456 additions and 127 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
7 changes: 7 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ dependencies {
implementation project(':easywaylocation')
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.maps.android:android-maps-utils:0.6.2'

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.prabhat.locationsample

import android.app.Activity
import android.content.Context
import android.view.View
import android.widget.TextView
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.Marker
import org.json.JSONObject
import java.lang.Exception
import java.text.DecimalFormat

class CustomInfoWindowForGoogleMap(context: Context): GoogleMap.InfoWindowAdapter {
private val df: DecimalFormat = DecimalFormat("0.00")

var mWindow = (context as Activity).layoutInflater.inflate(R.layout.marker_window, null)


override fun getInfoWindow(p0: Marker?): View {
render(p0, mWindow)
return mWindow
}

override fun getInfoContents(p0: Marker?): View {
render(p0, mWindow)
return mWindow
}

private fun render(marker: Marker?, mWindow: View) {

val title = mWindow.findViewById<TextView>(R.id.textView)
val time = mWindow.findViewById<TextView>(R.id.textView2)
val distance = mWindow.findViewById<TextView>(R.id.textView3)
try {
val json = JSONObject(marker?.title)
title.text = json.getString("placeSummary")
if (json.getString("time").isNotEmpty()){
time.visibility = View.VISIBLE
distance.visibility = View.VISIBLE
time.text = df.format(json.getString("timeFromPrevPoint").toDouble()/60) + " sec"
distance.text = df.format(json.getString("distanceFromPrevPoint").toDouble()/1609.344)+" mile"
}else{
time.visibility = View.GONE
distance.visibility = View.GONE
}
}catch (e:Exception){
e.printStackTrace()
}

}
}
129 changes: 102 additions & 27 deletions app/src/main/java/com/example/prabhat/locationsample/MapsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
package com.example.prabhat.locationsample

import android.graphics.Color
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.os.Handler
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.easywaylocation.draw_path.DirectionUtil
import com.example.easywaylocation.draw_path.PolyLineDataBean
import com.google.android.gms.maps.CameraUpdateFactory
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.BitmapDescriptorFactory
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.MarkerOptions
import com.google.maps.android.SphericalUtil
import kotlinx.android.synthetic.main.activity_maps.*


class MapsActivity : AppCompatActivity(), OnMapReadyCallback, DirectionUtil.DirectionCallBack {

lateinit var polyLineDetails:HashMap<String, PolyLineDataBean>
lateinit var directionUtil: DirectionUtil

companion object{
val WAY_POINT_TAG = "way_point_tag"
val ARC_POINT_TAG = "arc_point_tag"
val waypoint1 = LatLng(37.423669, -122.090168)
val waypoint2 = LatLng(37.420930, -122.085362)
val origin = LatLng(37.421481, -122.092156)
val destination = LatLng(37.421519, -122.086809)

val markerOptionsOrigin = MarkerOptions()
val markerOptionsDestination = MarkerOptions()

}

override fun pathFindFinish(
polyLineDetails: HashMap<String, PolyLineDataBean>,
polyLineDetailsArray: ArrayList<PolyLineDataBean>
) {
this.polyLineDetails = polyLineDetails
mMap.clear()
initAllMarker(polyLineDetailsArray)
mMap.addMarker(markerOptionsOrigin)
directionUtil.drawPath(WAY_POINT_TAG)
}

override fun pathFindFinish(polyLineDetails: HashMap<String, PolyLineDataBean>) {
for (i in polyLineDetails.keys){
Log.v("sample", polyLineDetails[i]?.time)
}
private fun initAllMarker(polyLineDetails: ArrayList<PolyLineDataBean>) {
markerOptionsOrigin.icon(BitmapDescriptorFactory.fromBitmap(getIcon(R.drawable.car_icon)));
markerOptionsOrigin.rotation(SphericalUtil.computeHeading(origin, waypoint1).toFloat())

for (data in polyLineDetails){
data.position?.let {
val markerOptionswayPoint = MarkerOptions()
markerOptionswayPoint.position(data.position!!)
markerOptionswayPoint.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin));
markerOptionswayPoint.title(data.toJson().toString())
mMap.addMarker(markerOptionswayPoint)

}

}

}

private lateinit var mMap: GoogleMap
Expand All @@ -42,30 +79,68 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, DirectionUtil.Dire

override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
val markerInfoWindowAdapter = CustomInfoWindowForGoogleMap(this)
googleMap.setInfoWindowAdapter(markerInfoWindowAdapter)
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)
.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)
wayPoints.add(waypoint1)
wayPoints.add(waypoint2)
directionUtil = DirectionUtil.Builder()
.setDirectionKey("xyz")
.setOrigin(origin)
.setWayPoints(wayPoints)
.setGoogleMap(mMap)
.setPathAnimation(true)
.setPolyLineWidth(5)
.setCallback(this)
.setDestination(destination)
.build()

val bean = PolyLineDataBean().also {
it.placeSummary = "Origin"
}
val bean2 = PolyLineDataBean().also {
it.placeSummary = "destination"
}
markerOptionsOrigin.position(origin)
markerOptionsOrigin.icon(BitmapDescriptorFactory.fromBitmap(getIcon(R.drawable.map_pin)));
markerOptionsOrigin.title(bean.toJson().toString())

val data = directionUtil.getShortestPathDetails(origin, destination)

markerOptionsDestination.position(destination)
markerOptionsDestination.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin));
markerOptionsDestination.title(data.toJson().toString())

directionUtil.drawArcDirection(origin, destination, 0.5, ARC_POINT_TAG)
mMap.addMarker(markerOptionsOrigin).tag = "origin"
mMap.addMarker(markerOptionsDestination)

button2.setOnClickListener {
directionUtil.clearPolyline(ARC_POINT_TAG)
directionUtil.initPath()
Handler().postDelayed({
try {
directionUtil.clearPolyline(WAY_POINT_TAG)
directionUtil.serOrigin(LatLng(37.422404, -122.091699),wayPoints)
directionUtil.setPathAnimation(false)
directionUtil.initPath()
}catch (erro:Exception){

}
},5000)
button2.text = "Complete Ride"
}
}


fun getIcon(id:Int):Bitmap{
val height = 100
val width = 100
val bitmapdraw: BitmapDrawable = resources.getDrawable(id) as BitmapDrawable
val b: Bitmap = bitmapdraw.getBitmap()
return Bitmap.createScaledBitmap(b, width, height, false)
}



}
Binary file added app/src/main/res/drawable/car_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/map_pin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 34 additions & 4 deletions app/src/main/res/layout/activity_maps.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
xmlns:android="http://schemas.android.com/apk/res/android">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:context=".MapsActivity" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="Start Ride"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".8" />

</androidx.constraintlayout.widget.ConstraintLayout>
47 changes: 47 additions & 0 deletions app/src/main/res/layout/marker_window.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#fff"
android:padding="8dp"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
android:textStyle="bold"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="@color/black"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="5dp"
android:textColor="@color/black"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit 9f20ba7

Please sign in to comment.