简体   繁体   中英

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. It is working perfectly. But it's not looking good. I want to move the icon from one location to a new location like Ola app and I am using car icon so I want turn icon when change the location into 90 degrees.

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();

private double bearingBetweenLocations(LatLng latLng1, LatLng latLng2) {
        final double PI = 3.14159;
        final double lat1 = latLng1.latitude * PI / 180;
        final double long1 = latLng1.longitude * PI / 180;
        final double lat2 = latLng2.latitude * PI / 180;
        final double long2 = latLng2.longitude * PI / 180;

        final double dLon = (long2 - long1);

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

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

@Override
    public void onLocationChanged(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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM