Skip to content Skip to sidebar Skip to footer

I Want To Move Icon From One Location Point To Another Point Where Icon Should Move One Location To Other In Android

I am working on a car tacking app. I want to move the icon to another location in android I had already tried to remove old map icon and clearing all icons and generating new icon.

Solution 1:

I assume you are using Google map marker to show the location. If so, no need to remove the marker. Instead save the marker object in a variable and update the variable whenever the location changes. Eg; marker.setPosition();

Solution 2:

privatedoublebearingBetweenLocations(LatLng latLng1, LatLng latLng2){
        finaldouble PI = 3.14159;
        finaldouble lat1 = latLng1.latitude * PI / 180;
        finaldouble long1 = latLng1.longitude * PI / 180;
        finaldouble lat2 = latLng2.latitude * PI / 180;
        finaldouble long2 = latLng2.longitude * PI / 180;

        finaldouble dLon = (long2 - long1);

        finaldouble y = Math.sin(dLon) * Math.cos(lat2);
        finaldouble 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;
    }

Calculate the degree of rotation.And then onLocationChanged() set the image again:

@Override
    publicvoidonLocationChanged(Location location) {
        if (location == null)
            return;
        if (mMap != null) {
            if (mPositionMarker != null && mPositionMarker.isVisible()) {
                mPositionMarker.remove();
            }
            newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
            if (oldLatLng != null) {
                mPositionMarker = mMap.addMarker(new MarkerOptions()
                        .flat(true)
                        .anchor(0.5f, 0.5f).icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.ic_cab_top)).rotation((float) bearingBetweenLocations(newLatLng, oldLatLng))
                        .position(new LatLng(location.getLatitude(), location
                                .getLongitude())));
                animateMarker(mPositionMarker, location); // Helper method for smooth animation
                mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
                        .getLatitude(), location.getLongitude())));
            }
            oldLatLng = newLatLng;
        }

Post a Comment for "I Want To Move Icon From One Location Point To Another Point Where Icon Should Move One Location To Other In Android"