-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a5c8c62
Showing
56 changed files
with
1,199 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Google Maps Directions API - polyline drawer helper | ||
|
||
## What is it? | ||
It's small library written in Kotlin to utilize Google Directions API calls | ||
and convert response to PolylineOptions, ready to add to GoogleMap. | ||
|
||
## Why should I use it? | ||
Well, you don't have to. But if you already reading this - | ||
consider using it if you don't want to parse nested jsons by yourself. | ||
|
||
## How do I use it? | ||
|
||
### Add dependency | ||
|
||
That's simple. First - add dependency to your build.gradle: | ||
|
||
You must also provide google maps core dependency: | ||
|
||
```groovy | ||
implementation : "com.google.android.gms:play-services-maps:11.8.0" | ||
``` | ||
|
||
### Create instance of DirectionsApiClient: | ||
|
||
```kotlin | ||
class AnotherBoringMapActivity : Activity(){ | ||
fun createClient() { | ||
directionsApiClient = DirectionsApiClient( | ||
apiKey = getString(R.string.google_directions_key)) | ||
} | ||
} | ||
``` | ||
|
||
### Request route between points | ||
```kotlin | ||
class AnotherBoringMapActivity : Activity(){ | ||
fun requestRoute(from : LatLng, to: LatLng){ | ||
directionsApiClient.getRoutePolylines(origin = from, dest = to) { | ||
//do something with list of PolylineOptions | ||
//remember, that you are not on main thread here | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Customize request! | ||
You can add some customization to Directions API request, just look at TransitOptions class. | ||
You can specify transit mode, and what to avoid during route planning. | ||
|
||
### Write your own parser! | ||
Yeah! That's possible! Just implement **Converter<GeocodedResponse?, List<PolylineOptions>>** and you are ready to go! | ||
|
||
### I still don't know hot to use it | ||
Look at example in this repo, in **app** directory. | ||
|
||
## I want to improve this library! | ||
Sure! Pull requests are welcome! Any reasonable extension will be added. | ||
|
||
## Tools used: | ||
* [Google Maps API](https://developers.google.com/maps/) | ||
* [Kotlin Coroutines](https://kotlinlang.org/docs/reference/coroutines.html) | ||
* [OkHttp](https://github.com/square/okhttp) | ||
* [Retrofit](http://square.github.io/retrofit/) | ||
* [Retrofit coroutines extensions](https://github.com/gildor/kotlin-coroutines-retrofit) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/* | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
apply plugin: 'kotlin-android' | ||
|
||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion 27 | ||
buildToolsVersion "27.0.3" | ||
defaultConfig { | ||
applicationId "me.rozkmin.googlemapsdraw" | ||
minSdkVersion 19 | ||
targetSdkVersion 27 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation supportDependencies.values() | ||
implementation mapsDependencies.googleMapsCore | ||
|
||
implementation project(":gmapspolyline") | ||
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<resources> | ||
<!-- | ||
TODO: Before you release your application, you need a Google Maps API key. | ||
To do this, you can either add your release key credentials to your existing | ||
key, or create a new key. | ||
Note that this file specifies the API key for the release build target. | ||
If you have previously set up a key for the debug target with the debug signing certificate, | ||
you will also need to set up a key for your release certificate. | ||
Follow the directions here: | ||
https://developers.google.com/maps/documentation/android/signup | ||
Once you have your key (it starts with "AIza"), replace the "google_maps_key" | ||
string in this file. | ||
--> | ||
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string> | ||
<string name="google_directions_key">DIRECTIONS_KEY_HERE</string> | ||
|
||
</resources> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="me.rozkmin.googlemapsdraw"> | ||
<!-- | ||
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use | ||
Google Maps Android API v2, but you must specify either coarse or fine | ||
location permissions for the 'MyLocation' functionality. | ||
--> | ||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<!-- | ||
The API key for Google Maps-based APIs is defined as a string resource. | ||
(See the file "res/values/google_maps_api.xml"). | ||
Note that the API key is linked to the encryption key used to sign the APK. | ||
You need a different API key for each encryption key, including the release key that is used to | ||
sign the APK for publishing. | ||
You can define the keys for the debug and release targets in src/debug/ and src/release/. | ||
--> | ||
<meta-data | ||
android:name="com.google.android.geo.API_KEY" | ||
android:value="@string/google_maps_key" /> | ||
|
||
<activity | ||
android:name=".MapsActivity" | ||
android:label="@string/title_activity_maps"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
84 changes: 84 additions & 0 deletions
84
app/src/main/java/me/rozkmin/googlemapsdraw/MapsActivity.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package me.rozkmin.googlemapsdraw | ||
|
||
import android.support.v7.app.AppCompatActivity | ||
import android.os.Bundle | ||
|
||
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.LatLng | ||
import com.google.android.gms.maps.model.Marker | ||
import com.google.android.gms.maps.model.MarkerOptions | ||
import com.google.android.gms.maps.model.Polyline | ||
import me.rozkmin.gmapspolyline.AVOID | ||
import me.rozkmin.gmapspolyline.DirectionsApiClient | ||
import me.rozkmin.gmapspolyline.MODE | ||
import me.rozkmin.gmapspolyline.TransitOptions | ||
|
||
class MapsActivity : AppCompatActivity(), OnMapReadyCallback { | ||
|
||
private lateinit var googleMap: GoogleMap | ||
private lateinit var directionsApiClient: DirectionsApiClient | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_maps) | ||
|
||
val mapFragment = supportFragmentManager | ||
.findFragmentById(R.id.map) as SupportMapFragment | ||
mapFragment.getMapAsync(this) | ||
|
||
directionsApiClient = DirectionsApiClient( | ||
apiKey = getString(R.string.google_directions_key), | ||
logHttp = true) | ||
} | ||
|
||
override fun onMapReady(googleMap: GoogleMap) { | ||
this.googleMap = googleMap | ||
|
||
val krk = LatLng(50.052, 19.944) | ||
this.googleMap.addMarker( | ||
MarkerOptions() | ||
.position(krk) | ||
.draggable(false) | ||
.title("Marker in Kraków")) | ||
|
||
this.googleMap.addMarker( | ||
MarkerOptions() | ||
.position(krk) | ||
.draggable(true) | ||
.title("Drag me") | ||
) | ||
|
||
this.googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(krk, 13f)) | ||
|
||
this.googleMap.setOnMarkerDragListener(object : GoogleMap.OnMarkerDragListener { | ||
override fun onMarkerDragEnd(marker: Marker) { | ||
getPolylineToRoute(marker.position, krk) | ||
} | ||
|
||
override fun onMarkerDragStart(p0: Marker?) {} | ||
|
||
override fun onMarkerDrag(p0: Marker?) {} | ||
}) | ||
} | ||
|
||
private val mutableList: MutableList<Polyline> = mutableListOf() | ||
|
||
fun getPolylineToRoute(from: LatLng, to: LatLng) { | ||
mutableList.forEach { it.remove() } | ||
mutableList.clear() | ||
|
||
val transitOptions = TransitOptions(mode = MODE.WALKING, whatToAvoidArray = arrayOf(AVOID.FERRIES, AVOID.HIGHWAYS)) | ||
|
||
directionsApiClient.getRoutePolylines(origin = from, dest = to, options = transitOptions) { | ||
runOnUiThread { | ||
it.forEach { | ||
//you can do additional polyline customization here | ||
mutableList.add(googleMap.addPolyline(it)) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.