Curve-Fit is an Android library for drawing curves on Google Maps

Overview

Curve-Fit

Android Arsenal Release License

Android library for drawing curves on Google Maps. This library uses Bezier cubic equation in order to compute all intermediate points of a curve.

Changes

  • Please note that this release only works with Google Maps Android SDK v2. If you are experimenting with Google Maps Android SDK 3.0.0 BETA, try using CurveFit-2.0.0-beta
  • [bug] Fixed (#4) (#6)

Demo

demo sample_india sample_denmark

Setup

Gradle

dependencies {
    implementation 'com.github.sarweshkumar47:curve-fit:1.1.1'
}

Maven

<dependency>
 <groupId>com.github.sarweshkumar47</groupId>
 <artifactId>curve-fit</artifactId>
 <version>1.1.1</version>
 <type>pom</type>
</dependency>

JitPack

allprojects {
    repositories {
    ...
    maven { url 'https://jitpack.io' }
    }
}

Add the dependency

dependencies {
    implementation 'com.github.sarweshkumar47:Curve-Fit:version_1_1_1'
}

Usage

In your activity's onCreate() method, use getMapAsync() to register for the map callback. Implement OnMapReadyCallback, OnCurveDrawnCallback, OnCurveClickListener interfaces and override the onMapReady() method

@Override
public void onMapReady(GoogleMap googleMap) {

    // Create a CurveManager object and pass googleMaps reference to it
    curveManager = new CurveManager(googleMap);

    // Register a callback to be invoked after curve is drawn on map
    curveManager.setOnCurveDrawnCallback(this);
    
    // Set a listener for curve click events.
    curveManager.setOnCurveClickListener(this);
        
    // Create a CurveOptions object and add atleast two latlong points to it
    // You can set different options in CurveOptions object similar to PolyLineOptions
    CurveOptions curveOptions = new CurveOptions();
    curveOptions.add(new LatLng(12.9715987, 77.5945627));
    curveOptions.add(new LatLng(12.2958104, 76.6393805));
    curveOptions.color(Color.DKGRAY);
    curveOptions.setAlpha(0.5f);
    curveOptions.width(12);
    List<PatternItem> pattern = Arrays.asList(new Dash(30), new Gap(30));
    curveOptions.pattern(pattern);
    curveOptions.geodesic(false);

    map.addMarker(new MarkerOptions().position(sourceLatLng).anchor(0.5f, 1f));
    map.addMarker(new MarkerOptions().position(destinationLatLng).icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)).anchor(0.5f, 1f));

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(12.64779202, 77.16562563), 14));
    
    // Draws curve asynchronously
    curveManager.drawCurveAsync(curveOptions);
}

Remove listeners in order to prevent memory leaks.

@Override
protected void onDestroy() {
    if (curveManager != null) {
        curveManager.unregister();
        curveManager = null;
    }
    if (map != null) {
        map.stopAnimation();
        map.clear();
        map = null;
    }
    if (mapFragment != null) {
        mapFragment.getMapAsync(null);
        mapFragment = null;
    }
    super.onDestroy();
}

Check example projects for more info.

Advanced usage

CurveOptions

  • Alpha
CurveOptions setAlpha(float alpha)

Defines shape and deviation of a curve. Alpha can vary from -1 to 1. The below images demonstrate how alpha value is used to define the shape and deviation of a curve in the algorithm (default value is 0.5).

alpha_negative

Note: This behaviour may vary when a curve passes through 180 degree meridian. Sometimes, you may not get a perfect curve. In that case, use setComputePointsBasedOnScreenPixels(true) method to get the desired curve.

  • ComputePointsBasedOnScreenPixels
CurveOptions setComputePointsBasedOnScreenPixels(boolean computePointsBasedOnPixels)

If set to true, geographic location points will be converted to screen pixel points and algorithm uses screen pixel points to compute all intermediate curve points. Refer the below image to understand the variations.

Note: Method setComputePointsBasedOnScreenPixels(true) gives a perfect curve regardless of the geographical locations. But it has certain limitations. This method requires more computation time. Sometimes, you may a get a strangely shaped curve when the two lat-long points are off the screen (outside the visible region) or when two points are too close at lower zoom levels. Workaround to this problem is to compute the lat-long bounds and zoom the map camera towards the center of the bounds.

Contributions

Contributions are welcome and much appreciated.

Credit

Icons and images

License

Copyright 2018 Sarweshkumar C R

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Random NPE

    Random NPE

    Periodically, I'll get a NullPointerException when closing a map with a curve and then reopening it to show the curve again. I haven't isolated a pattern yet. Here is the stack trace:

    java.lang.NullPointerException: Attempt to invoke interface method 'void com.makesense.labs.curvefit.interfaces.UiThreadCallback.publishToUiThread(android.os.Message)' on a null object reference at com.makesense.labs.curvefit.impl.WorkerHandlerThread$1.handleMessage(WorkerHandlerThread.java:83) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.os.HandlerThread.run(HandlerThread.java:65)

    bug 
    opened by ryust 12
  • Confused

    Confused

    How is this integrated into a project? Via a gradle library dependency (like the old one) or put in the libs directory as an APK? I can't get it to work.

    question 
    opened by ryust 7
  • Curve click error

    Curve click error

    Got this error trying to click a curve after an app sleep/resume... maybe also related to the weak references issues...

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.get(java.lang.Object)' on a null object reference
        at com.makesense.labs.curvefit.impl.CurveManager$DelegatingCurveClickListener.onPolylineClick(CurveManager.java:142)
        at com.google.android.gms.maps.zzq.zza(Unknown Source:2)
        at com.google.android.gms.maps.internal.zzbg.dispatchTransaction(Unknown Source:5)
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source:12)
        at android.os.Binder.transact(Binder.java:667)
        at hl.b(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):14)
        at com.google.android.gms.maps.internal.bu.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):4)
        at com.google.maps.api.android.lib6.impl.dt.y(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):270)
        at com.google.maps.api.android.lib6.gmm6.api.r.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):137)
        at com.google.maps.api.android.lib6.gmm6.api.q.a(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):58)
        at com.google.maps.api.android.lib6.gmm6.vector.af.b(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):130)
        at com.google.maps.api.android.lib6.gmm6.vector.dc.onSingleTapConfirmed(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):22)
        at com.google.maps.api.android.lib6.impl.gesture.c.onSingleTapConfirmed(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):240)
        at com.google.maps.api.android.lib6.impl.gesture.e.handleMessage(:com.google.android.gms.dynamite_mapsdynamite@[email protected] (100400-257258062):9)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    
    bug 
    opened by ryust 4
  • show just once

    show just once

    hi there ! thanks for useful library , i have one problem and one question 1 - in my case i have few item when click on item show you dialog with map fragment and show the curved polyline but thats work for first time and the next time the polyline dosn't show . 2 - can i set animation to polyline color change ?!

    bug question 
    opened by meysam2594 2
  • Not compatible with Android Maps SDK 3.0 beta

    Not compatible with Android Maps SDK 3.0 beta

    As the title says, this library is not compatible with the new Android Maps SDK coming out. Any idea if/when a compatible version is coming out?

    I really like this library, but will have to pull it from my app if this can't be resolved soon.

    enhancement 
    opened by ryust 2
Releases(version_1_1_1)
Owner
Sarweshkumar C R
Self-taught Android developer with experience in Embedded Systems - Open source enthusiast - True Gamer
Sarweshkumar C R
Android Maps Extensions is a library extending capabilities of Google Maps Android API v2.

Android Maps Extensions Library extending capabilities of Google Maps Android API v2. While Google Maps Android API v2 is a huge leap forward comapare

Maciej Górski 408 Dec 15, 2022
Scale bar for Android Maps (Google Maps, OSM, MapBox, Yandex)

Map Scale View Scale view for any Android Maps SDK (not only Google Maps) Contributing I encourage you to participate in this project. Feel free to op

Stas Parshin 109 Nov 18, 2022
Maps application in Android Studio using the Maps SDK for android

Google-Maps-Application Maps application in Android Studio using the Maps SDK for android. This app was initially developed in Java, but then was conv

Kyle McInnis 0 Nov 30, 2021
🍃 Organic Maps is an Android & iOS offline maps app for travelers, tourists, hikers, and cyclists based on top of crowd-sourced OpenStreetMap data and curated with love by MapsWithMe founders.

?? Organic Maps is an Android & iOS offline maps app for travelers, tourists, hikers, and cyclists based on top of crowd-sourced OpenStreetMap data and curated with love by MapsWithMe founders. No ads, no tracking, no data collection, no crapware.

Organic Maps 4.3k Dec 31, 2022
malik dawar 87 Sep 18, 2022
Demo de uso de google maps en Android, charla para el GDG Chimbote

mapasbasico Demo de uso de google maps en Android, charla para el GDG Chimbote Puedes usar este proyecto como base para trabajar con mapas en Android.

Whiston Kendrick Borja Reyna 4 Sep 17, 2021
An android app that uses Google Maps API and SDK to track a user's location and calculate the total distance travelled

Bike Rush is an android app that uses Google Maps API and SDK to track a user's location and calculate the total distance travelled by him or her along with time and average speed.

Ishant Chauhan 21 Nov 14, 2022
Google Maps Api test using marker rotation and routes.

Google Maps Api test using marker rotation and routes. Features ✔️ Kotlin ✔️ DI: Hilt ✔️ Retrofit ✔️ Gson ✔️ View binding ✔️ Coroutines ✔️ AndroidX ✔️

Carlos Adan 39 Jul 15, 2022
EasyRoutes allows you to easily draw routes through the google maps address api.

EasyRoutes EasyRoutes allows you to easily draw routes through the google maps address api. Note: You need to generate an API key from the google cons

Antonio Huerta Reyes 7 Jul 26, 2022
Membuat Custom Tooltip Marker Google Maps

Custom-Tooltip-Marker Membuat Custom Tooltip Marker Google Maps Tutorial Build with Android Studio https://youtu.be/E8ND0YThNiU Tutorial Build with St

Azhar Rivaldi 5 Feb 17, 2022
An app to search nearby businesses on Google Maps & Add Grocery Items to List!

GoStore: Internship Program Project A mobile app is built where the user can search for his nearby locations based on his requirement. Whenever the us

null 7 Nov 28, 2022
Maps SDK for Android Utility Library

Maps SDK for Android Utility Library Description This open-source library contains utilities that are useful for a wide range of applications using th

Google Maps 3.4k Dec 30, 2022
Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.

Interactive, thoroughly customizable maps in native Android powered by vector tiles and OpenGL.

Mapbox 247 Dec 27, 2022
TileOverlay component for react-native-maps

TileOverlay component for react-native-maps

Joe Julik 1 Apr 10, 2022
App usage tracker which maps your app usage to geo location.

Guilt Guilt is an inspiration from Meta (pun intended), it tracks the apps usage and maps it with geo location data where the app was last used. The a

null 6 Dec 28, 2022
This is a repo for implementing Map pan or drag (up, down, right ,left) to load more places on the Google Map for Android

Challenge Display restaurants around the user’s current location on a map ○ Use the FourSquare Search API to query for restaurants: https://developer.

Mahmoud Ramadan 7 Jul 30, 2022
Android library project that lets you manage the location updates to be as painless as possible

Smart Location Library Android library project that intends to simplify the usage of location providers and activity recognition with a nice fluid API

Nacho Lopez 1.6k Dec 29, 2022
Android library that allows you to determine your location in a few of lines

locsimple Android library that allows you to determine your location in some lines! Benefits: automatic processing of permissions processing of enabli

Dmitry 7 Aug 31, 2022
A library for reverse geocoding coordinates in Android

?? AndroidReverseGeocoder A library for reverse geocoding coordinates in Android. The library is built on top of android's Geocoder library. AndroidRe

ShimShim27 1 Jun 24, 2022