Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
rozkminiacz committed Feb 23, 2018
0 parents commit a5c8c62
Show file tree
Hide file tree
Showing 56 changed files with 1,199 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
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
19 changes: 19 additions & 0 deletions .idea/gradle.xml

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

49 changes: 49 additions & 0 deletions .idea/misc.xml

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

10 changes: 10 additions & 0 deletions .idea/modules.xml

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

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

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

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

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

64 changes: 64 additions & 0 deletions README.md
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)
8 changes: 8 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/*
.DS_Store
/build
/captures
.externalNativeBuild
33 changes: 33 additions & 0 deletions app/build.gradle
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"
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
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
22 changes: 22 additions & 0 deletions app/src/debug/res/values/google_maps_api.xml
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>
41 changes: 41 additions & 0 deletions app/src/main/AndroidManifest.xml
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 app/src/main/java/me/rozkmin/googlemapsdraw/MapsActivity.kt
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))
}
}
}
}
}
Loading

0 comments on commit a5c8c62

Please sign in to comment.