24

मैं मानचित्र पर चलने वाली कार मार्कर अनुकरण करने के लिए चिकनी संक्रमण को कार्यान्वित करना चाहता हूं।एंड्रॉइड मानचित्र एपीआई वी 2 में मार्कर को एनिमेट कैसे करें?

क्या एंड्रॉइड मानचित्र एपीआई v2 में मार्कर को एनिमेट करना संभव है?

+0

मैं समझता हूँ के रूप में आप देशी नक्शे एपीआई V2 व्यवहार की जरूरत है एक स्थान से दूसरे स्थान तक मार्कर स्थानांतरित करने के लिए सुचारू रूप से? –

+0

आप इस पोस्ट को देख सकते हैं http://stackoverflow.com/questions/13728041/move-markers-in-google-map-v2-android –

+0

संभावित डुप्लिकेट [एंड्रॉइड पर मानचित्र में जोड़े जाने पर मार्कर को एनिमेट करने के लिए कैसे करें? ] (http://stackoverflow.com/questions/8191582/how-to-animate-marker-when-it-is-added-to-map-on-android) – bummi

उत्तर

24

Google मानचित्र V2 पर मार्कर को एनिमेट करने के लिए नीचे दिए गए कोड को आज़माएं। आप एनीमेशन के रूप में नीचे के लिए हैंडलर में मार्कर पर एनीमेशन लागू करते हैं और इसे संभाल करने के लिए Interpolator वर्ग उपयोग करने की आवश्यकता:

public void animateMarker(final Marker marker, final LatLng toPosition, 
     final boolean hideMarker) { 
    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    Projection proj = mGoogleMapObject.getProjection(); 
    Point startPoint = proj.toScreenLocation(marker.getPosition()); 
    final LatLng startLatLng = proj.fromScreenLocation(startPoint); 
    final long duration = 500; 
    final Interpolator interpolator = new LinearInterpolator(); 
    handler.post(new Runnable() { 
     @Override 
     public void run() { 
      long elapsed = SystemClock.uptimeMillis() - start; 
      float t = interpolator.getInterpolation((float) elapsed 
        /duration); 
      double lng = t * toPosition.longitude + (1 - t) 
        * startLatLng.longitude; 
      double lat = t * toPosition.latitude + (1 - t) 
        * startLatLng.latitude; 
      marker.setPosition(new LatLng(lat, lng)); 
      if (t < 1.0) { 
       // Post again 16ms later. 
       handler.postDelayed(this, 16); 
      } else { 
       if (hideMarker) { 
        marker.setVisible(false); 
       } else { 
        marker.setVisible(true); 
       } 
      } 
     } 
    }); 
} 
+0

बहुत बहुत धन्यवाद। वह बेहद सहायक था। – Anil

+0

आप इस कोड को वास्तव में कैसे लागू करते हैं? @ एनील? क्या आप जीपीएस निर्देशांक के साथ मार्कर चल सकते हैं? कृपया उत्तर पोस्ट करें। – momokjaaaaa

+2

अरे, बहुत बहुत धन्यवाद। यह काम करता हैं। लेकिन मार्कर स्टार्ट मार्कर एनीमेशन से पहले आगे बढ़ता है। क्या आप कृपया मुझे बताएं कि इसे कैसे ठीक किया जाए। – Sadia

4

बस एक संस्करण लागू किया, इस

public class MarkerAnimation { 
static GoogleMap map; 
ArrayList<LatLng> _trips = new ArrayList<>() ; 
Marker _marker; 
LatLngInterpolator _latLngInterpolator = new LatLngInterpolator.Spherical(); 

public void animateLine(ArrayList<LatLng> Trips,GoogleMap map,Marker marker,Context current){ 
    _trips.addAll(Trips); 
    _marker = marker; 

animateMarker(); 
} 

    public void animateMarker() { 
     TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() { 
      @Override 
      public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) { 
       return _latLngInterpolator.interpolate(fraction, startValue, endValue); 
      } 
     }; 
     Property<Marker, LatLng> property = Property.of(Marker.class, LatLng.class, "position"); 

     ObjectAnimator animator = ObjectAnimator.ofObject(_marker, property, typeEvaluator, _trips.get(0)); 

     //ObjectAnimator animator = ObjectAnimator.o(view, "alpha", 0.0f); 
     animator.addListener(new Animator.AnimatorListener() { 
      @Override 
      public void onAnimationCancel(Animator animation) { 
       // animDrawable.stop(); 
      } 

      @Override 
      public void onAnimationRepeat(Animator animation) { 
       // animDrawable.stop(); 
      } 

      @Override 
      public void onAnimationStart(Animator animation) { 
       // animDrawable.stop(); 
      } 

      @Override 
      public void onAnimationEnd(Animator animation) { 
       // animDrawable.stop(); 
       if (_trips.size() > 1) { 
        _trips.remove(0); 
        animateMarker(); 
       } 
      } 
     }); 

     animator.setDuration(300); 
     animator.start(); 
    } 
कोशिश

LatLngInterpolator क्लास Google लोगों द्वारा पूर्व-लिखित है जिसे आप निम्नानुसार उपयोग कर सकते हैं:

public interface LatLngInterpolator { 

public LatLng interpolate(float fraction, LatLng a, LatLng b); 

public class Spherical implements LatLngInterpolator { 
    @Override 
    public LatLng interpolate(float fraction, LatLng from, LatLng to) { 
     // http://en.wikipedia.org/wiki/Slerp 
     double fromLat = toRadians(from.latitude); 
     double fromLng = toRadians(from.longitude); 
     double toLat = toRadians(to.latitude); 
     double toLng = toRadians(to.longitude); 
     double cosFromLat = cos(fromLat); 
     double cosToLat = cos(toLat); 

     // Computes Spherical interpolation coefficients. 
     double angle = computeAngleBetween(fromLat, fromLng, toLat, toLng); 
     double sinAngle = sin(angle); 
     if (sinAngle < 1E-6) { 
      return from; 
     } 
     double a = sin((1 - fraction) * angle)/sinAngle; 
     double b = sin(fraction * angle)/sinAngle; 

     // Converts from polar to vector and interpolate. 
     double x = a * cosFromLat * cos(fromLng) + b * cosToLat * cos(toLng); 
     double y = a * cosFromLat * sin(fromLng) + b * cosToLat * sin(toLng); 
     double z = a * sin(fromLat) + b * sin(toLat); 

     // Converts interpolated vector back to polar. 
     double lat = atan2(z, sqrt(x * x + y * y)); 
     double lng = atan2(y, x); 
     return new LatLng(toDegrees(lat), toDegrees(lng)); 
    } 

    private double computeAngleBetween(double fromLat, double fromLng, double toLat, double toLng) { 
     // Haversine's formula 
     double dLat = fromLat - toLat; 
     double dLng = fromLng - toLng; 
     return 2 * asin(sqrt(pow(sin(dLat/2), 2) + 
       cos(fromLat) * cos(toLat) * pow(sin(dLng/2), 2))); 
    } 
} 
} 

फिर MarkerAnimation वर्ग की एक वस्तु का दृष्टांत और इस तरह विधि कॉल:

MarkerAnimation.animateLine(TripPoints,map,MovingMarker,context); 
17

प्रदान की संस्करणों में से कोई भी मेरे लिए काम किया, इसलिए मैं अपने कस्टम समाधान क्रियान्वित किया है। यह दोनों स्थान और रोटेशन एनीमेशन प्रदान करता है।

/** 
* Method to animate marker to destination location 
* @param destination destination location (must contain bearing attribute, to ensure 
*     marker rotation will work correctly) 
* @param marker marker to be animated 
*/ 
public static void animateMarker(Location destination, Marker marker) { 
    if (marker != null) { 
     LatLng startPosition = marker.getPosition(); 
     LatLng endPosition = new LatLng(destination.getLatitude(), destination.getLongitude()); 

     float startRotation = marker.getRotation(); 

     LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed(); 
     ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1); 
     valueAnimator.setDuration(1000); // duration 1 second 
     valueAnimator.setInterpolator(new LinearInterpolator()); 
     valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override public void onAnimationUpdate(ValueAnimator animation) { 
       try { 
        float v = animation.getAnimatedFraction(); 
        LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition); 
        marker.setPosition(newPosition); 
        marker.setRotation(computeRotation(v, startRotation, destination.getBearing())); 
       } catch (Exception ex) { 
        // I don't care atm.. 
       } 
      } 
     }); 

     valueAnimator.start(); 
    } 
} 

एनीमेशन के निर्दिष्ट अंश के लिए रोटेशन गणना। मार्कर जिस दिशा शुरू से अंत रोटेशन के करीब है में घुमाया जाता है:

private static float computeRotation(float fraction, float start, float end) { 
    float normalizeEnd = end - start; // rotate start to 0 
    float normalizedEndAbs = (normalizeEnd + 360) % 360; 

    float direction = (normalizedEndAbs > 180) ? -1 : 1; // -1 = anticlockwise, 1 = clockwise 
    float rotation; 
    if (direction > 0) { 
     rotation = normalizedEndAbs; 
    } else { 
     rotation = normalizedEndAbs - 360; 
    } 

    float result = fraction * rotation + start; 
    return (result + 360) % 360; 
} 

और अंत में गूगल के LatLngInterpolator:

private interface LatLngInterpolator { 
    LatLng interpolate(float fraction, LatLng a, LatLng b); 

    class LinearFixed implements LatLngInterpolator { 
     @Override 
     public LatLng interpolate(float fraction, LatLng a, LatLng b) { 
      double lat = (b.latitude - a.latitude) * fraction + a.latitude; 
      double lngDelta = b.longitude - a.longitude; 
      // Take the shortest path across the 180th meridian. 
      if (Math.abs(lngDelta) > 180) { 
       lngDelta -= Math.signum(lngDelta) * 360; 
      } 
      double lng = lngDelta * fraction + a.longitude; 
      return new LatLng(lat, lng); 
     } 
    } 
} 
+0

मैं इसे लागू करने की कोशिश कर रहा हूं कि मैं गंतव्य पर असर कैसे जोड़ूं ?, क्या आपके पास अंतिम परियोजना के जिथब पर कोई स्रोत है? –

+0

@ फ्रैंकऑडूम आपको 'स्थान गंतव्य' पैरामीटर पर 'सेटबियरिंग (फ्लोट)' या 'असर (स्थान) 'विधि के माध्यम से सही ढंग से असर सेट करना चाहिए। – skywall

+0

यह मुझे मिला सबसे अच्छा जवाब है। बिल्कुल कोई झटका नहीं है। –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^