0

i am trying to place a marker to a map according to location, and my activity refreshes after every 10 sec so that I can get location from database every 10.
What I am expecting is to change marker location every 10 sec but marker keeps on changing position frequently that is 5 to 6 times every 10 sec.

And my cab marker also keeps on dissapearing I this its the map.clear(), but without it 2 to 4 markers keeps on reappearing

For placing markers

LatLngBounds.Builder builder = new LatLngBounds.Builder();

                    ArrayList<Marker> markers = new ArrayList<>();

                    mMap.clear();                                                   // to clear markers and polylines

                    drivLat1 = driverLocation.getLatitude();
                    drivLng1 = driverLocation.getLongitude();
                    drivLat2 = driverChangedLocation.getLatitude();
                    drivLng2 = driverChangedLocation.getLongitude();

                    bearingBetweenLocations(drivLat1,drivLng1,drivLat2,drivLng2);

                    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.car_marker);

                    markers.add(0,mMap.addMarker(new MarkerOptions().position(new LatLng(driverLocation.getLatitude(), driverLocation.getLongitude()))
                            .icon(icon)
                            .title("Driver Location")));



                    markers.add(1,mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("User Location")));

                    rotateMarker(markers.get(0), (float) bearingBetweenLocations(drivLat1,drivLng1,drivLat2,drivLng2));
                    for (Marker marker : markers) {

                        builder.include(marker.getPosition());
                    }
                    LatLngBounds bounds = builder.build();

                    int padding = 100;

                    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
                    mMap.animateCamera(cu);

private double bearingBetweenLocations(double lat11 ,double long11,double lat22 ,double long22) {

    double PI = 3.14159;
    double lat1 = lat11 * PI / 180;
    double long1 = long11 * PI / 180;
    double lat2 = lat22 * PI / 180;
    double long2 = long22 * PI / 180;

    double dLon = (long2 - long1);

    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
            * Math.cos(lat2) * Math.cos(dLon);

    double brng = Math.atan2(y, x);

    brng = Math.toDegrees(brng);
    brng = (brng + 360) % 360;

    return brng;
}

private void rotateMarker(final Marker marker, final float toRotation) {

    if (!isMarkerRotating) {
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final float startRotation = marker.getRotation();
        final long duration = 1000;

        final Interpolator interpolator = new LinearInterpolator();

        handler.post(new Runnable() {
            @Override
            public void run() {
                isMarkerRotating = true;

                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float) elapsed / duration);

                float rot = t * toRotation + (1 - t) * startRotation;

                marker.setRotation(-rot > 180 ? rot / 2 : rot);
                if (t < 1.0) {
                    // Post again 16ms later.
                    handler.postDelayed(this, 16);
                } else {
                    isMarkerRotating = false;
                }
            }
        });
    }
}

And this is how I set accuracy, bcoz I think that this can be a problem

if (location.hasAccuracy()) {
        // Accuracy is in rage of 20 meters, stop listening we have a fix
        if (location.getAccuracy() < 20) {
            LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
        }
    }

What I want is that markers only change position when data gets updated

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
dhami_ji
  • 162
  • 3
  • 12
  • Where is your code where you update the location? – Eselfar Dec 11 '17 at 15:16
  • @Eselfar Thanks for your reply .But I have solved this querie I was calling a function before setting the marker . But could u please look at my bearing code is it right ,bcoz I am getting degrees I dont know whether it is right or not ,bcoz my marker keeps on changing its position as location updates that is ever second. Or is there any library to calculate bearing accoding to previous and new latitudes and longitudes – dhami_ji Dec 11 '17 at 16:25
  • Have a look at that https://stackoverflow.com/a/8050255/1827254 – Eselfar Dec 11 '17 at 16:48
  • @Eselfar it shows distance between 2 location . what I want is to raotate a marker according to user previous and current location . Could u please look at this answer and please tell me how to set the values to mPathPolygonPoints . https://stackoverflow.com/a/40686476/8040956 – dhami_ji Dec 11 '17 at 16:52

0 Answers0