2016-02-09 9 views
11

मैं एक लैटलिंग से दूसरे में मार्कर को स्थानांतरित करना चाहता हूं। मैंने इस कोड का उपयोग दूरी और समय के आधार पर आसानी से मार्कर को स्थानांतरित करने के लिए किया है।ड्राइविंग दिशाओं के बाद 2 निर्देशांक के बीच एक मार्कर को ले जाएं

public void animateMarker(final LatLng toPosition, 
           final boolean hideMarker) { 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     Projection proj = googleMap.getProjection(); 
     Point startPoint = proj.toScreenLocation(cabMarker.getPosition()); 
     final LatLng startLatLng = proj.fromScreenLocation(startPoint); 

     final Interpolator interpolator = new LinearInterpolator(); 

     handler.post(new Runnable() { 
      @Override 
      public void run() { 

       Location prevLoc = new Location("service Provider"); 
       prevLoc.setLatitude(startLatLng.latitude); 
       prevLoc.setLongitude(startLatLng.longitude); 

       Location newLoc = new Location("service Provider"); 
       newLoc.setLatitude(toPosition.latitude); 
       newLoc.setLongitude(toPosition.longitude); 

       System.out.println("Locations ---- " + prevLoc + "-" + newLoc); 

       float bearing = prevLoc.bearingTo(newLoc); 

       long elapsed = SystemClock.uptimeMillis() - start; 
       float t = interpolator.getInterpolation((float) elapsed 
         /CAB_TRACK_INTERVAL); 
       double lng = t * toPosition.longitude + (1 - t) 
         * startLatLng.longitude; 
       double lat = t * toPosition.latitude + (1 - t) 
         * startLatLng.latitude; 
       cabMarker.setPosition(new LatLng(lat, lng)); 
       cabMarker.setRotation(bearing + 90); 

       if (t < 1.0) { 
        // Post again 16ms later. 
        handler.postDelayed(this, 16); 
       } else { 
        if (hideMarker) { 
         cabMarker.setVisible(false); 
        } else { 
         cabMarker.setVisible(true); 
        } 
       } 
      } 
     }); 


    } 

लेकिन समस्या यह मार्कर अभ्यस्त ड्राइविंग निर्देश आगे बढ़ने लेकिन एक सीधी रेखा में से बी करने के लिए एक यह ए और बी के बीच में लगभग 10 सड़क मध्य बिन्दुओं और फिर तथा उस पथ पर ले जाने के लिए संभव है है?

+0

http://ddewaele.github.io/ GoogleMapsV2WithActionBarSherlock/part3 इस लिंक के माध्यम से जाएं –

+0

जब आप एनिमेटमार्क विधि को कॉल करते हैं? –

+0

मैं एपीआई से दो स्थानों के रूप में परिणाम प्राप्त करने के बाद इसे कॉल करता हूं। मैं बस इस विधि को स्थान पास करता हूं और यह चलता है। – Akshat

उत्तर

0
import com.google.android.gms.maps.model.LatLng; 

import static java.lang.Math.asin; 
import static java.lang.Math.atan2; 
import static java.lang.Math.cos; 
import static java.lang.Math.pow; 
import static java.lang.Math.sin; 
import static java.lang.Math.sqrt; 
import static java.lang.Math.toDegrees; 
import static java.lang.Math.toRadians; 

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

    public class Linear implements LatLngInterpolator { 
     @Override 
     public LatLng interpolate(float fraction, LatLng a, LatLng b) { 
      double lat = (b.latitude - a.latitude) * fraction + a.latitude; 
      double lng = (b.longitude - a.longitude) * fraction + a.longitude; 
      return new LatLng(lat, lng); 
     } 
    } 

    public 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); 
     } 
    } 

    public class Spherical implements LatLngInterpolator { 

     /* From github.com/googlemaps/android-maps-utils */ 
     @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))); 

एनीमेशन प्रभाव के लिए इस कोड को आज़माएं।

public class MarkerAnimation { 
    static void animateMarkerToGB(final Marker marker, final LatLng finalPosition, final LatLngInterpolator latLngInterpolator) { 
     final LatLng startPosition = marker.getPosition(); 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     final Interpolator interpolator = new AccelerateDecelerateInterpolator(); 
     final float durationInMs = 3000; 

     handler.post(new Runnable() { 
      long elapsed; 
      float t; 
      float v; 

      @Override 
      public void run() { 
       // Calculate progress using interpolator 
       elapsed = SystemClock.uptimeMillis() - start; 
       t = elapsed/durationInMs; 
       v = interpolator.getInterpolation(t); 

       marker.setPosition(latLngInterpolator.interpolate(v, startPosition, finalPosition)); 

       // Repeat till progress is complete. 
       if (t < 1) { 
        // Post again 16ms later. 
        handler.postDelayed(this, 16); 
       } 
      } 
     }); 
    } 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    static void animateMarkerToHC(final Marker marker, final LatLng finalPosition, final LatLngInterpolator latLngInterpolator) { 
     final LatLng startPosition = marker.getPosition(); 

     ValueAnimator valueAnimator = new ValueAnimator(); 
     valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator animation) { 
       float v = animation.getAnimatedFraction(); 
       LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, finalPosition); 
       marker.setPosition(newPosition); 
      } 
     }); 
     valueAnimator.setFloatValues(0, 1); // Ignored. 
     valueAnimator.setDuration(3000); 
     valueAnimator.start(); 
    } 

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
    static void animateMarkerToICS(Marker marker, LatLng finalPosition, final LatLngInterpolator latLngInterpolator) { 
     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, finalPosition); 
     animator.setDuration(3000); 
     animator.start(); 
    } 
} 
      } 
     } 
    } 
-1

सबसे पहले आप बी को गूगल दिशाओं से बिंदु A से मार्ग का अनुरोध करने के लिए है और यह एक पाली लाइन जो अंक होते हैं वापस आ जाएगी आप तो बात करने के लिए जब तक आप सभी का चक्कर है हर बिंदु से एनीमेशन विधि कॉल चाहते अंक और उसके बाद आप केवल एक बिंदु से बी से एनिमेट करते समय मार्कर को एक निश्चित मार्ग से गुजरने के लिए मार्कर बना सकते हैं, पहले Google दिशाओं के बारे में Google से एपीआई पढ़ें और इसे

+0

यदि आपको और मदद चाहिए तो कृपया –

+0

पर बताएं मेरे पास कोड है –

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