Created
October 14, 2020 09:47
-
-
Save mksantoki/bcd03ef880909bd21f15ddcf1a4e6d4e to your computer and use it in GitHub Desktop.
how to draw an arc line on google map?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 com.google.maps.android.SphericalUtil | |
val optionsForeground: PolylineOptions? = null | |
fun showCurvedPolyline( | |
googleMap: GoogleMap, | |
startPoint: LatLng, | |
endPoint: LatLng, | |
k: Double, | |
routeColor: Int | |
): Polyline? { | |
//Calculate distance and heading between two points | |
val d = SphericalUtil.computeDistanceBetween(startPoint, endPoint) | |
val h = SphericalUtil.computeHeading(startPoint, endPoint) | |
//Midpoint position | |
val p: LatLng = SphericalUtil.computeOffset(startPoint, d * 0.5, h) | |
//Apply some mathematics to calculate position of the circle center | |
val x = (1 - k * k) * d * 0.5 / (2 * k) | |
val r = (1 + k * k) * d * 0.5 / (2 * k) | |
val c: LatLng = SphericalUtil.computeOffset(p, x, h + 90.0) | |
//Polyline options | |
val options = PolylineOptions() | |
// val pattern: List<PatternItem> = arrayListOf(Dash(30f), Gap(20f)) | |
//Calculate heading between circle center and two points | |
val h1 = SphericalUtil.computeHeading(c, startPoint) | |
val h2 = SphericalUtil.computeHeading(c, endPoint) | |
//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) | |
options.add(pi) | |
} | |
//Draw polyline | |
return googleMap.addPolyline(options.width(10f).color(routeColor).geodesic(false)) | |
} | |
fun showNormalPolyline( | |
googleMap: GoogleMap, | |
startPoint: LatLng, | |
endPoint: LatLng, | |
routeColor: Int | |
): Polyline? { | |
val options = PolylineOptions() | |
options.add(startPoint) | |
options.add(endPoint) | |
return googleMap.addPolyline(options.width(10f).color(routeColor).geodesic(false)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment