Location util : Location « Hardware « Android






Location util

 
//package com.android.gnview.utils;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Point;
import android.location.Location;
import android.location.LocationManager;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.Projection;

/**
 * Classe de m?thodes utilitaire pour la localisation.
 * @author Antoine Belliard
 *
 */
public class LocUtils {

  /**
   * M?thode pour retourn?e la distance entre deux Location
   * @param a
   * @param b
   * @return la distance en m ou km
   */
  public static String distanceBeetwin(Location a, Location b) {
    double dist = a.distanceTo(b);
    String unit;
    if (dist > 1000) {
      unit = " km";
      dist /= 1000;
      dist = ((int) (dist*100)) / 100.0;
    } else {
      unit = " m";
      dist = ((int) (dist*100)) / 100;
    }
    return (dist + unit);
  }
  /**
   * M?thode qui retourne le plus pr?cis de tous les appareils de localisation
   * @param lManager le manager
   * @return le provider le plus pr?cis
   */
  public static String getBestLocationProvider(LocationManager lManager) {
    List <String> providers = lManager.getProviders(true);
    String choixSource = null;
    if (providers.isEmpty()){
      return null;
    }else if(providers.size()==1){
      return providers.get(0);
    }else{
      int i = Integer.MAX_VALUE;
      for(String provider : providers) {
        if (lManager.getProvider(provider).getAccuracy()<i && lManager.getProvider(provider).getAccuracy() > 0 ) {
          choixSource = provider;
          i = lManager.getProvider(provider).getAccuracy();
        }
      }
    }
    return choixSource;
  }

  /**
   * M?thode de conversion d'une location en GeoPoint
   * @param location une localisation
   * @return le GeoPoint de m?me position
   */
  public static GeoPoint toGeoPoint(Location location) {
    return new GeoPoint( (int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6) );
  }

  /**
   * @param myLocation
   * @return
   */
  public static Location toLocation(GeoPoint myLocation) {
    Location loc = new Location("");
    loc.setLatitude(myLocation.getLatitudeE6()/1E6);
    loc.setLongitude(myLocation.getLongitudeE6()/1E6);
    return loc;
  }
  /**
   * Appel l'algorithme de Douglas-Peucker
   * @param projection la projection pour avoir des points graphiques
   * @param line la ligne ? simplifi?e
   * @param d l'erreur
   * @return la liste de location simplifi?e
   */
  public static ArrayList<Location> DouglasPeucker(Projection projection, ArrayList<Location> line, double d) {
    ArrayList<Point> listPoints = GeoUtils.DouglasPeucker(toPoints(projection,line),d);
    return toLineOfInterest(projection, listPoints);
  }
  /**
   * Convertit une liste de Point en liste de Location
   * @param projection la projection ? appliquer
   * @param listPoints la liste de point
   * @return une lineOfInterest
   */
  private static ArrayList<Location> toLineOfInterest(Projection projection, ArrayList<Point> listPoints) {
    ArrayList<Location> retLine = new ArrayList<Location>();
    for (Point p:listPoints) {
      retLine.add( toLocation(projection.fromPixels(p.x, p.y)) );
    }
    return retLine;
  }
  /**
   * Convertit une liste de location en une liste de Point
   * @param projection la projection ? appliquer
   * @param line la liste ? convertir
   * @return une liste de Points
   */
  private static List<Point> toPoints(Projection projection, ArrayList<Location> line) {
    ArrayList<Point> retPoints = new ArrayList<Point>();
    Point prev = new Point();
    for (Location l:line) {
      Point p = new Point();
      projection.toPixels(toGeoPoint(l), p);
      if (!p.equals(prev.x, prev.y)) {
        retPoints.add(p);
      }
      prev = p;
    }
    return retPoints;
  }
  /**
   * V?rifie si le GPS est activ?
   * @param lManager le location manager ? v?rifi?
   * @return true si le GPS est activ?, false sinon
   */
  public static boolean isGPSEnabled(LocationManager lManager) {
    List<String> EnablesProviders = lManager.getProviders(true);
    for (String provider:EnablesProviders) {
      if (provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER))
        return true;
    }
    return false;
  }

}

   
  








Related examples in the same category

1.Location service and LocationManager
2.Location service
3.Using LocationManager
4.My location
5.Display GEO location
6.Using location service for the weather
7.Using Intent to go to a geo location
8.Location based service
9.My location and Google Map
10.Custom Location Overlay
11.Get my location
12.Geo location and Google Map
13.Location Tracking
14.A light pool of objects that can be resused to avoid allocation.
15.extends android.location.Location
16.Geo Location Util
17.C:\Java_Dev\WEB\dev\android\weatherforecastsystem-read-only\com\hci\pwf\LocationUtil.java
18.upload Data with Geo location
19.Copy a file from one location to another.
20.LocationManager.GPS_PROVIDER