Coder Social home page Coder Social logo

ubercaranimation's Introduction

UberCarAnimation

A demo application which demonstrates movement of car on map developed after inspiration from Uber.

Demo


Youtube Link: https://www.youtube.com/watch?v=JIs4kLZ8qQI

APIs and Libraries used
  • Google Maps Api
  • Google Maps Directions API
  • Volley



Explained Logic

Steps:

  • Parse the "overview_polyline" from the JSON by providing the appropriate GET parameters. For eg:
    
    "https://maps.googleapis.com/maps/api/directions/json?" +
                        "mode=driving&"
                        + "transit_routing_preference=less_driving&"
                        + "origin=" + latitude + "," + longitude + "&"
                        + "destination=" + destination + "&"
                        + "key=" + getResources().getString(R.string.google_directions_key)
    
    
  • Decode the polyline which will provide you with list of latitudes and longitudes that is List<LatLng> to be apt.
  • Setting up of Value animator:Create a value animator by providing the ofFloatValue, setting duration and adding update listener in Handler
    
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
    valueAnimator.setDuration(3000);
    valueAnimator.setInterpolator(new LinearInterpolator());
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator valueAnimator) {
        //CODE
    

    });

  • In the value animator Update listener get the Animation fraction and evaluate the latitudes and longitudes as shown:
    
    v=valueAnimator.getAnimatedFraction();
    lng = v * endPosition.longitude + (1 - v)* startPosition.longitude;
    lat = v * endPosition.latitude + (1 - v)* startPosition.latitude;
    
    
    where v is animation fraction and startposition and endPostion refer to each pair of LatLng from the decoded list from polyline for eg (0,1) then (1,2) then(2,3) and so on.
    According to linear interpolation: The parameter 'v' defines where to estimate the value on the interpolated line, it is 0 at the first point and 1 and the second point. For interpolated values between the two points v ranges between 0 and 1. We evaluate values one by one between each pair of LatLng by traversing through the list.
  • Finally set position of marker to the new position, also evaluating the bearing between the consecutive points so that it seems car is turning literally and finally update camera as:
    
    marker.setPosition(newPos);
    marker.setAnchor(0.5f, 0.5f);
    marker.setRotation(getBearing(startPosition, newPos));
    mMap.moveCamera(CameraUpdateFactory
                    .newCameraPosition
                    (new CameraPosition.Builder()
                    target(newPos)
                    .zoom(15.5f)
                    .build()));
    
    



Running the project

The application uses Google Maps Api Key and Google Map Directions key. Get these api key on google developers console after enabling them for your project. Replace your google maps directions api key in strings.xml and google maps key in google_maps_api.xml. For convenience a TODO has been added there just follow them.

Driver mode

The driver mode is the real world example of the situation where the driver app is communicating with user app and the car is animating accordingly.
Youtube Link: https://www.youtube.com/watch?v=-gTGJF7mZQI
Here the python script is acting like a driver for the user app.

Explained Logic

  • Establish a MQTT broker by logging into one of the MQTT providers. I used CloudMQTT.
  • Create the instance for MQTT and generate credentials.
  • Integrate the MQTT Paho Client for android by including following in your app module build.gradle:
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
    implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
    
  • Fill your credentials in MQTTHelper class. The username, password and the server URI which is of form tcp://uri:port.
  • Similarly add them in UberMQTT.py file.
  • The Python script will be acting as driver and publishing the location on MQTT server in flex interval of 1 seconds using topic of location/track. The android app will connect to the broker and subscribe to the topic of kind location/+. As soon as the MQTT server receives the location it will push it to the android client.
  • We will receive location in form of String which will be converted to LatLng type by function convertStringToLatLng.
  • Then RxRelay is used to create stream of the LatLng's. Now as we need pair of LatLng for dispatching animation we will be taking buffer operator with count 2. This is shown below: In messageReceived callback:
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
     String payload = new String(message.getPayload());
     LatLng currentLatLng = convertStringToLatLng(payload);
     Log.d(TAG, topic + " " + currentLatLng);
     latLngPublishRelay.accept(currentLatLng);
    }
    
    And subscribing to this relay with buffer 2:
    latLngPublishRelay
       .buffer(2)
       .observeOn(AndroidSchedulers.mainThread())
       .subscribe(new Consumer>() {
    
     @Override
     public void accept(List<LatLng> latLngs) throws Exception {
      emission++;
      animateCarOnMap(latLngs);
     }
    

    });

  • As soon as the Relay will emit two LatLng the animateCarOnMap function will dispatch animation through ValueAnimator. This animation will be based on same logic as was explained above.

Developers

If you found this code demo helpful or you learned something today and want to thank me, consider buying me a cup of โ˜• at PayPal

ubercaranimation's People

Contributors

amanjeetsingh150 avatar imgurpreetsk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ubercaranimation's Issues

Camera Not Zooming in out

Hello Sir Thanks for such nice example, it is really helpful and useful. I have issue regarding the camera zoom in and out. When the car is moving camera is just moving with the car. I want to zoom in and zoom the map. I tried to figure out from the code but failed to do it. Please can you brief me regarding this issue.

Polyline and animation doesnt display

Hi, I have added my own API keys and the map displays fine.
But when I try to navigate to "jhilmil metro station", it only puts the marker on it, and no polyline shows or car. As you can see in this screenshot https://ibb.co/gNp7fK

Only thing I have changed is that targetSDK is 27 (even tried with 28)

MQTT problem

How to run MQTT server and i have already username ,pass and server url but i don;t understand

Callback when location reached

Thank you for your great project.

I wanted to ask how can I know when the car reaches its destination ? Can we get any callback listener for that ?

Thanks.

Trouble with MQTT client

I'm having trouble connecting with the MQTT client and hence my application is not working as its supposed to.
I plugged in my API generated keys from the google console and the MQTT credentials but not getting the expected result. Please help

Need Help

Hello can u tell me how i rotate car on given path according to driver location.Suppose i have to go to Source A to Destination B.Route i got from direction api .Driver lat long also on same starting position i want to navigate driver car according to lat long of route api.

missing files

there is no string file or res/values/google_maps_api.xm

An inquiry to how the animation works.

I am opening an issue to inquire as to whether I can use this guide and get realtime animation updates as I track the driver moving in the real world.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.