From 9f20ba7bbc9f971694622d2190a5682b8f090e70 Mon Sep 17 00:00:00 2001 From: prabhat rai Date: Sat, 25 Dec 2021 02:19:20 +0530 Subject: [PATCH] fix major bug and add more features --- .idea/caches/build_file_checksums.ser | Bin 597 -> 594 bytes .idea/codeStyles/Project.xml | 7 + .idea/compiler.xml | 2 +- .idea/gradle.xml | 1 + .idea/misc.xml | 2 +- app/build.gradle | 2 + .../CustomInfoWindowForGoogleMap.kt | 51 ++++ .../prabhat/locationsample/MapsActivity.kt | 129 +++++++-- app/src/main/res/drawable/car_icon.png | Bin 0 -> 1812 bytes app/src/main/res/drawable/map_pin.png | Bin 0 -> 459 bytes app/src/main/res/layout/activity_maps.xml | 38 ++- app/src/main/res/layout/marker_window.xml | 47 +++ .../draw_path/DirectionUtil.kt | 269 ++++++++++++------ .../easywaylocation/draw_path/MapAnimator.kt | 10 + .../draw_path/PolyLineDataBean.kt | 20 +- .../easywaylocation/draw_path/PolylineBean.kt | 5 + 16 files changed, 456 insertions(+), 127 deletions(-) create mode 100644 app/src/main/java/com/example/prabhat/locationsample/CustomInfoWindowForGoogleMap.kt create mode 100644 app/src/main/res/drawable/car_icon.png create mode 100644 app/src/main/res/drawable/map_pin.png create mode 100644 app/src/main/res/layout/marker_window.xml create mode 100644 easywaylocation/src/main/java/com/example/easywaylocation/draw_path/PolylineBean.kt diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index e605b1c6d311329c1e39aa1069badf0cdd867c61..c1735088657a5af2a77509fc12458e46f5f77554 100644 GIT binary patch delta 119 zcmV--0Eqw91kwbMmj!*g$v6&?oUapB-+GIReXV&juBMnZA=aUE01T5Z0u~S&K6wUo zl7;bf!3$;J&(ds@%mE!C-yNtub1!f(axgP(ag%QW ZHk11SN)R#K>@BG5XreiiVqqsGw|K*vF);uD delta 134 zcmV;10D1q?1l0tPmj!si$_S{DoUbHp?Xue!jzP}RgniJG3YD9504pz5b7gXKFK}{U zVrXG>a$#wc!T~muT>%#uSD0=o7H`?%b=#Z3jDp$$bN~=+Z)0I>lhFYc6FY*=Ta}B% o9F5l@$IQ(H2y_4ulgj}clN16j5KXKF>@!2=2pB!8y>IAUc$&O8*#H0l diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 681f41a..7643783 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,6 +1,10 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 61a9130..fb7f4a8 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 4e6b049..40d2787 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -6,6 +6,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 3b019c9..a0875ae 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' + } diff --git a/app/src/main/java/com/example/prabhat/locationsample/CustomInfoWindowForGoogleMap.kt b/app/src/main/java/com/example/prabhat/locationsample/CustomInfoWindowForGoogleMap.kt new file mode 100644 index 0000000..bebcf38 --- /dev/null +++ b/app/src/main/java/com/example/prabhat/locationsample/CustomInfoWindowForGoogleMap.kt @@ -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(R.id.textView) + val time = mWindow.findViewById(R.id.textView2) + val distance = mWindow.findViewById(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() + } + + } +} \ No newline at end of file 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 fee933e..0155318 100644 --- a/app/src/main/java/com/example/prabhat/locationsample/MapsActivity.kt +++ b/app/src/main/java/com/example/prabhat/locationsample/MapsActivity.kt @@ -1,9 +1,9 @@ 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 @@ -11,21 +11,58 @@ 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 + 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, + polyLineDetailsArray: ArrayList + ) { + this.polyLineDetails = polyLineDetails + mMap.clear() + initAllMarker(polyLineDetailsArray) + mMap.addMarker(markerOptionsOrigin) + directionUtil.drawPath(WAY_POINT_TAG) } - override fun pathFindFinish(polyLineDetails: HashMap) { - for (i in polyLineDetails.keys){ - Log.v("sample", polyLineDetails[i]?.time) - } + private fun initAllMarker(polyLineDetails: ArrayList) { + 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 @@ -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) + } + } diff --git a/app/src/main/res/drawable/car_icon.png b/app/src/main/res/drawable/car_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..f1f1a1a6768ec956635d334a1eafbe18120f788f GIT binary patch literal 1812 zcmeHH{a4a=6#f7Ts9;8xX;@ISWu?(@W@$>O1+9Z`wD1LH7AjUiK=UQjuW1X<%9k?I znT45c&G)itrp8>PGtFdarYmilW{RRPhYfT1Q@`#H=$_}^d+t5YPtU#Q$p~QX!CE?4 z0sycKx^IxFLyL(Jvx#e~H%L4j#$FZE+f zgN0GjxPxLIh>MGJ;R}vP4jmNnT!iB2f=N#Y0N`5#_ObjH78XqWPw`2C|EYkgCxL9D z)=Pt!!2kwFm?2SU%o3ZWINRkb>{i-4tin4Ii0eq}Ki}ZAanojJmo4P26j!%x+dX%B zc~f`Me0DRK{sF>*;;b|MJzq;9o<-Z$?JP-c4vGr?k^EI{oZFhPn6u8t1<%esB7FL>9w$7c0I` z(?90T3dAF|6iOuSn4ENsZpFW2+a@om*$_qx>w>{dc zyWonXb87acuHkCSl!v6FaC`J5@0|BYZtP=sd23zC4pqBecHo7rJ7xcfpSO1u9&VpS zTrJ#uHlpo(t`1vEykdzUDliV>GlFEe>w0-?Q)jxHvNv`ub(!nU%%~66F-)?Bg6T2n zrAm6>r-4)_%%El2bRg4EKJo5ss95q%^qd&w=sUPSXSC{jc0?U1oWd;w2jz-*~7 zsabon!`m4Cc#>dje2qi(f{(JcVpYp5D)IjOG`V z4n+A~a5YSEsdA=$PF`eI*9ps$4Ds_cAkls+)D5USi#?M-d0b3QfBJ*;RT@6RxB(uo zY=upnMiYWi&;zBh^D{?K_WhcGW<4P35Y}(uy{$`nOM#_g5jD`01_N1Gyi!@^rl&Kl z-XPCtI;@hZ14m}cL(2mJj@mQp6J4{A7X`Xj`gqYXDO@}&m+I0G^T~1inKd0X!7-F2isJ)6NYm8})Ntc7m z4Kw3B0>`mGKE*RrZIS($Q(tPOxV(_)mQ}d}98HCn$2fun+TD}GkXN;pWg#jRLy&@G z5Ap*Ifxg);3O9v>eeo@NU=%rOicF?aH4^o6&_7VHDZGhJ-+`1yzX} z-MG`p9mu>~X9uT)GWsTkEXs@G8e1?x|8}7KUf5%!pxHyoDSbG4;NBN)H$qj9!h-FO zx6c(DhxX|Hu7eHOa{IhI8=p;#y`EKzQv2~KaBZX-r);!DEqW2dkLg=Mi#YusAs;OU literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable/map_pin.png b/app/src/main/res/drawable/map_pin.png new file mode 100644 index 0000000000000000000000000000000000000000..bb8708657e7a556da8a32c6a3aeba0065962a622 GIT binary patch literal 459 zcmV;+0W|)JP)@}@HfUKt!u#~{sz7WbY(4A3PKm?54^*;4*U!Mn_7ZUt$yGjxc`UU zI*aQrXbS?T!Ck7O;I2Ju!Ji;7nz3LkxQiIC>Jj&UG4L)2zOtk!nJNXrCv0I3)75h& z@Rb_DNq&~QAlzum;*x|~f5(E2yl`m6&jRpY)l4CM#bFNk3Hpk{Q;pyz1AZ&6Y9Y-_ zT2u?KNUGWtt$16@wPwLntxCS+=_BB&zE$V*=mos_y1R%HYoq`G002ovPDHLkV1ga& B!KeTL literal 0 HcmV?d00001 diff --git a/app/src/main/res/layout/activity_maps.xml b/app/src/main/res/layout/activity_maps.xml index 4d0590c..266c700 100644 --- a/app/src/main/res/layout/activity_maps.xml +++ b/app/src/main/res/layout/activity_maps.xml @@ -1,8 +1,38 @@ - \ No newline at end of file + xmlns:android="http://schemas.android.com/apk/res/android"> + + + +