Android Open Source - clusterkraf Random Points Provider






From Project

Back to project page clusterkraf.

License

The source code is released under:

Apache License

If you think the Android project clusterkraf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.twotoasters.clusterkraf.sample;
//from w  w w.  j  a  va2  s  .c  o  m
import java.lang.ref.WeakReference;
import java.text.NumberFormat;
import java.util.ArrayList;

import android.os.AsyncTask;

import com.google.android.gms.maps.model.LatLng;
import com.twotoasters.clusterkraf.InputPoint;

class RandomPointsProvider {

  private static RandomPointsProvider instance;

  private WeakReference<GenerateCallback> generateCallback;
  private ArrayList<InputPoint> points;

  static RandomPointsProvider getInstance() {
    if (instance == null) {
      instance = new RandomPointsProvider();
    }
    return instance;
  }

  void generate(GenerateCallback callback, GeographicDistribution geographicDistribution, int count) {
    this.points = null;
    this.generateCallback = new WeakReference<GenerateCallback>(callback);
    new GenerateTask(geographicDistribution).execute(count);
  }

  private void onGenerateTaskFinished(ArrayList<InputPoint> points) {
    this.points = points;
    if (generateCallback != null) {
      GenerateCallback callback = generateCallback.get();
      if (callback != null) {
        callback.onRandomMarkersGenerated(points);
      }
      generateCallback = null;
    }
  }

  boolean hasPoints() {
    return points != null;
  }

  ArrayList<InputPoint> getPoints() {
    return points;
  }

  class GenerateTask extends AsyncTask<Integer, Void, ArrayList<InputPoint>> {

    private final double northeastLat;
    private final double northeastLng;
    private final double southwestLat;
    private final double southwestLng;

    GenerateTask(GeographicDistribution geographicDistribution) {
      switch(geographicDistribution) {
        case NearTwoToasters:
          LatLng ttLatLng = MarkerData.TwoToasters.getLatLng();
          northeastLat = ttLatLng.latitude + 0.2d;
          northeastLng = ttLatLng.longitude + 0.2d;
          southwestLat = ttLatLng.latitude - 0.2d;
          southwestLng = ttLatLng.longitude - 0.2d;
          break;
        case Worldwide:
        default:
          northeastLat = 90d;
          northeastLng = 180d;
          southwestLat = -90d;
          southwestLng = -180d;
          break;
      }
    }

    @Override
    protected ArrayList<InputPoint> doInBackground(Integer... params) {
      ArrayList<InputPoint> inputPoints = new ArrayList<InputPoint>(params[0]);
      NumberFormat nf = NumberFormat.getInstance();
      for (int i = 0; i < params[0]; i++) {
        MarkerData marker;
        if (i == 0) {
          marker = MarkerData.TwoToasters;
        } else {
          marker = new MarkerData(getRandomLatLng(), nf.format(i));
        }
        InputPoint point = new InputPoint(marker.getLatLng(), marker);
        inputPoints.add(point);
      }
      return inputPoints;
    }

    private LatLng getRandomLatLng() {
      double latitude = getRandomBetween(southwestLat, northeastLat);
      double longitude = getRandomBetween(southwestLng, northeastLng);
      return new LatLng(latitude, longitude);
    }

    private double getRandomBetween(double min, double max) {
      return min + (Math.random() * ((max - min)));
    }

    @Override
    protected void onPostExecute(ArrayList<InputPoint> result) {
      super.onPostExecute(result);
      onGenerateTaskFinished(result);
    }

  }

  enum GeographicDistribution {
    NearTwoToasters, Worldwide
  }

  interface GenerateCallback {
    void onRandomMarkersGenerated(ArrayList<InputPoint> result);
  }
}




Java Source Code List

android.UnusedStub.java
com.twotoasters.clusterkraf.AnimatedTransition.java
com.twotoasters.clusterkraf.BasePoint.java
com.twotoasters.clusterkraf.ClusterPoint.java
com.twotoasters.clusterkraf.ClusterTransitionsAnimation.java
com.twotoasters.clusterkraf.ClusterTransitionsBuildingTask.java
com.twotoasters.clusterkraf.ClusterTransitions.java
com.twotoasters.clusterkraf.ClusteringOnCameraChangeListener.java
com.twotoasters.clusterkraf.ClusteringTask.java
com.twotoasters.clusterkraf.Clusterkraf.java
com.twotoasters.clusterkraf.ClustersBuilder.java
com.twotoasters.clusterkraf.InfoWindowDownstreamAdapter.java
com.twotoasters.clusterkraf.InputPoint.java
com.twotoasters.clusterkraf.MarkerOptionsChooser.java
com.twotoasters.clusterkraf.OnInfoWindowClickDownstreamListener.java
com.twotoasters.clusterkraf.OnMarkerClickDownstreamListener.java
com.twotoasters.clusterkraf.Options.java
com.twotoasters.clusterkraf.sample.AdvancedModeFragment.java
com.twotoasters.clusterkraf.sample.DelayedIndeterminateProgressBarRunnable.java
com.twotoasters.clusterkraf.sample.MainActivity.java
com.twotoasters.clusterkraf.sample.MarkerData.java
com.twotoasters.clusterkraf.sample.NormalModeFragment.java
com.twotoasters.clusterkraf.sample.RandomPointsProvider.java
com.twotoasters.clusterkraf.sample.SampleActivity.java
com.twotoasters.clusterkraf.sample.SingleChoiceDialogFragment.java
com.twotoasters.clusterkraf.sample.ToastedMarkerOptionsChooser.java
com.twotoasters.clusterkraf.sample.ToastedOnMarkerClickDownstreamListener.java
com.twotoasters.clusterkraf.sample.TwoToastersActivity.java
com.twotoasters.clusterkraf.util.Distance.java