-1

Please I know if I use current location that time many answer I was seen. But My location come form service that is main problem. After service I will get all start point and end point. Now how can I animate this start point to end point? I already complete 0 index to 1 index moving with animation, but rest of this I did not. Now I need help to resolve this.

I have a location which I get from my service and I create route map in google maps. But Now I want to show a moving marker or a vehicle and animate it through start point to end point.

enter image description here

How can I implement an animation from start to the end point?This is my method and I show the route start point to end point with a polyline.

    private void setMapMarker(GoogleMap googleMap) {
    googleMap.clear();
    MapsInitializer.initialize(Objects.requireNonNull(getActivity()).getApplicationContext());
    api = ApiClient.getClient().create(ApiInterface.class);

    LocationInfo locationInfo = new LocationInfo();
    locationInfo.setMOV_DATE(fromDate.getText().toString());
    locationInfo.setEMPLOYE_ID(employeeID);
    locationInfo.setSTART_TIME(fromTime.getText().toString());
    locationInfo.setEND_TIME(toTime.getText().toString());

    Call<List<RouteList>> listCall = api.getLocation(locationInfo);
    APIHelper.enqueueWithRetry(listCall, new Callback<List<RouteList>>() {
        @Override
        public void onResponse(Call<List<RouteList>> call, Response<List<RouteList>> response) {
            try {
                if (response.isSuccessful()) {
                    List<RouteList> list = response.body();

                    Map<String, List<RouteList>> map = getEmployeeList(list);

                    if (list == null || list.isEmpty() || list.equals(0)) {
                        googleMap.clear();
                        Toast.makeText(getActivity(), "No data found", Toast.LENGTH_SHORT).show();
                    } else {
                        for (Map.Entry<String, List<RouteList>> entry : map.entrySet()) {
                            String employee = entry.getKey();
                            List<RouteList> list1 = map.get(employee);

                            if (list1.size() > 0) {

                                List<LatLng> latlng = new ArrayList<>();
                                for (int i = 0; i < list1.size(); i++) {
                                    double lat = Double.parseDouble(list1.get(i).getM_LATITUDE().trim());
                                    double lng = Double.parseDouble(list1.get(i).getM_LONGITDE().trim());
                                    String name = list1.get(i).getEMGIS_TIME();
                                    latlng.add(new LatLng(lat, lng));
                                    LatLng mLatlng = new LatLng(lat, lng);

                                    if (googleMap != null) {
                                        googleMap.addMarker(new MarkerOptions().position(mLatlng).title(name).snippet(""));
                                        cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 12.0f);
                                       googleMap.animateCamera(cameraUpdate);

                                }

                                PolylineOptions rectOptions = new PolylineOptions().addAll(latlng);
                                rectOptions.color(generator.getRandomColor());
                                Objects.requireNonNull(googleMap).addPolyline(rectOptions);

                            }
            } catch (Exception e) {
                Toast.makeText(getActivity(), "Error:" + e, Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<List<RouteList>> call, Throwable t) {
            call.cancel();
            Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

already show route map.

Babu
  • 39
  • 1
  • 8
  • I assume you want to move the marker as the vehicle is moving? To do that you will have to get the lat/long every second or so, then update the marker accordingly. If you want to animate it without the vehicle moving, you need to get lat/long positions between the start and end point and move the marker to that points. – HB. Jul 06 '19 at 06:00
  • I already got route and marker. But I want to show some animation on my route. My updated code see again. Thanks – Babu Jul 06 '19 at 06:24
  • Do you want to show the animation while the car is driving? – HB. Jul 06 '19 at 06:45
  • I have start point and end point. And I also I indicate the route. But I want to show some thing which go start point to end point as like car moving. – Babu Jul 06 '19 at 06:48
  • Have a look at my first comment. – HB. Jul 06 '19 at 06:53
  • Yes. But how can I move one marker to another ? any idea or example? – Babu Jul 06 '19 at 07:00
  • https://github.com/amanjeetsingh150/UberCarAnimation – Basi Jul 06 '19 at 08:15
  • Possible duplicate of [Rotate Marker and Move Animation on Map like Uber Android](https://stackoverflow.com/questions/35554796/rotate-marker-and-move-animation-on-map-like-uber-android) – MrUpsidown Jul 06 '19 at 10:20
  • Possible duplicate of https://stackoverflow.com/questions/33885378/animate-a-carmarker-along-a-path-in-google-map-android/33885379#33885379 – MrUpsidown Jul 06 '19 at 10:21
  • Possible duplicate of https://stackoverflow.com/questions/49667906/animate-or-move-marker-along-with-rout-path-on-googlemap – MrUpsidown Jul 06 '19 at 10:21

1 Answers1

0

I would firstly like to say that you should be more clear in your question.

It is important to know if you want to set the marker as the vehicle is driving. I will assume this is what you want to do and I will provide an answer according to this.


You already have a method that sets the marker -> setMapMarker

All you need to do now is to call this method every second or so.
You can do this by using a Thread, as shown below:

//Declare Thread
Thread thread;

void startSetMarker() {
    thread = new Thread() {
        @Override
        public void run() {
            try {
                while (!thread.isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                            public void run() {
                                //Call setMapMarker here
                            }
                        });
                    }
                } catch (InterruptedException e) {
            }
        }
    };
    thread.start();
}

//You can cancel the thread like this:

void cancelThread(){
    thread.interrupt();
}

By doing this, the marker will be updated every second.

When you want to start setting the marker you can call startSetMarker(); and when the user reaches the end destination, you can cancel the thread by calling cancelThread();

HB.
  • 3,942
  • 4
  • 24
  • 48
  • Thanks a lot. Firstly I appreciate your answer. But when I get lat long from my service that time how can I manage this? – Babu Jul 06 '19 at 08:16