Location distanceTo - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Location distanceTo method.

This code example shows how to use the following methods:bearingTo, distanceTo, getAltitude, hasAccuracy
6
CodeRank
This code example shows how to use the following methods:distanceTo, getAccuracy, getLatitude, getLongitude, getProvider
      Log.wtf(getClass().getSimpleName(),
              "Got null for getBestLocation()"); 
    } 
    else { 
      Log.i(getClass().getSimpleName(),
            String.format("%s %f:%f (%f meters)", loc.getProvider(),
                          loc.getLatitude(), loc.getLongitude(),
                          loc.getAccuracy()));
    } 
  } 
 
  private Location getBestLocation() {
    Location gps=mgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location network=
        mgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 
    // start off by handling cases where we only have one 
 
    if (gps == null) {
      return(network);

9
CodeRank
This code example shows how to use the following methods:distanceTo, setLatitude, setLongitude
	  //รับค่าละติจูด ลองกิจูดจากจุดที่ผู้ใช้เลือก เพื่อนำมาสร้าง Location สำหรับใช้ในการคำนวณหาระยะห่าง 
	  latSelect=item.getPoint().getLatitudeE6()/1E6;
	  longSelect=item.getPoint().getLongitudeE6()/1E6;
	   
	  //สร้าง Location เก็บตำแหน่งที่ผู้ใช้เลือก สำหรับใช้ในการคำนวณหาระยะห่าง 
	  Location ploc=new Location("plo1");
      ploc.setLatitude(latSelect);
      ploc.setLongitude(longSelect);
       
      //สร้าง Location เก็บตำแหน่งปัจจุบัน สำหรับใช้ในการคำนวณหาระยะห่าง 
	  Location ploc2=new Location("plo2");
      ploc2.setLatitude(HelloMap.curLat);
      ploc2.setLongitude(HelloMap.curLong);
       
      //คำนวณระยะห่างระหว่างตำแหน่งที่ผู้ใช้เลือกกับตำแหน่งปัจจุบัน (หน่วยที่ได้เป็นเมตร) 
      float dist=ploc.distanceTo(ploc2)/1000;
	   
      //สร้าง AlertDialog แสดงข้อความจากตำแหน่งที่ผู้ใช้เลือก พร้อมทั้งระยะห่างจากตำแหน่งปัจจุบัน 
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	  dialog.setTitle(item.getTitle());
	  dialog.setMessage(item.getSnippet()+"\n"+String.valueOf(dist)+" KiloMeters");
6
CodeRank
This code example shows how to use the following methods:distanceTo
	private double distance = 0.0;
 
    @Override 
    public void onLocationChanged(Location location) {
        if (lastKnownLocation != null && location != null) {
            distance = lastKnownLocation.distanceTo(location);
            /* 
            HashMap<String, Double> m = new HashMap<String, Double>(); 
            m.put("distanceTraveled", distance);*/ 
            //FlurryAgent.logEvent("LocationChanged", m); 
            /*CharSequence text = "Location change with distance = " + distance; 
            Toast.makeText(context, text, Toast.LENGTH_LONG).show();*/ 
        } 
        lastKnownLocation = location;
    } 
 
    @Override 
    public void onProviderDisabled(String provider) {
        requestLocationUpdates(); 
    } 
6
CodeRank
This code example shows how to use the following methods:distanceTo, setLatitude, setLongitude
     * getDistance: determines the distance, in miles, between this Restaurant and the GeoPoint param 
     * @param pointA: the point from which the distance to the restaurant is being measured 
     * @return: the distance, in miles, between the two points. Is a float.  
     */ 
    public double getDistance(GeoPoint pointA){
        Location locationA = new Location("point A");
     
        locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
        locationA.setLongitude(pointA.getLongitudeE6() / 1E6);
     
        Location locationB = new Location("point B");
     
        locationB.setLatitude(mLocation.getLatitudeE6() / 1E6);
        locationB.setLongitude(mLocation.getLongitudeE6() / 1E6);
     
        return roundDouble(locationA.distanceTo(locationB)/1609.34);//return in miles, not meters
    } 
     
    /** 
     * roundDouble: Takes in a Double value and rounds it to a single decimal place 
     * @param d: the Double value 
6
CodeRank
This code example shows how to use the following methods:distanceTo, setLatitude, setLongitude
    // Use average meters per pixel value from image corner to corner 
    float[] imageCorners = { 0, 0, imageWidth, imageHeight, imageWidth, 0, 0, imageHeight };
    double imageDistance = Math.sqrt(imageWidth * imageWidth + imageHeight * imageHeight);
 
    convertImageToGeoCoordinates(imageCorners);
    Location geo1 = new Location("compute");
    geo1.setLatitude(imageCorners[1]);
    geo1.setLongitude(imageCorners[0]);
    Location geo2 = new Location("compute");
    geo2.setLatitude(imageCorners[3]);
    geo2.setLongitude(imageCorners[2]);
    double geoDistance1 = geo1.distanceTo(geo2);
 
    geo1.setLatitude(imageCorners[5]);
    geo1.setLongitude(imageCorners[4]);
    geo2.setLatitude(imageCorners[7]);
    geo2.setLongitude(imageCorners[6]);
    double geoDistance2 = geo1.distanceTo(geo2);
 
    metersPerPixel = (float) (geoDistance1 + geoDistance2) / (float) (2 * imageDistance);

6
CodeRank
This code example shows how to use the following methods:distanceTo, getAccuracy, getElapsedRealtimeNanos
        if (from == null) {
            return true; 
        } 
 
        // if new location is older than the current one, the device hasn't moved. 
        if (to.getElapsedRealtimeNanos() < from.getElapsedRealtimeNanos()) {
            return false; 
        } 
 
        // Get the distance between the two points. 
        float distance = from.distanceTo(to);
 
        // Get the total accuracy radius for both locations. 
        float totalAccuracy = from.getAccuracy() + to.getAccuracy();
 
        // If the distance is greater than the combined accuracy of the two 
        // points then they can't overlap and hence the user has moved. 
        return distance >= totalAccuracy;
    } 
 
6
CodeRank
This code example shows how to use the following methods:distanceTo, getAccuracy, getAltitude, hasAccuracy, hasAltitude
	public void onProviderDisabled(String provider) {
	} 
 
	public void getGPSData() { 
		if (actualLocation != null) {
			if (actualLocation.hasAltitude()) {
				altitude = (String.valueOf(actualLocation.getAltitude()));
			} else { 
				altitude = " - ";
			} 
			if (actualLocation.hasAccuracy()) {
				accuracy = String.valueOf(round(actualLocation.getAccuracy()));
			} else { 
				accuracy = " - ";
			} 
		} 
	} 
 
	private static float round(float toRound) {
		return Math.round(toRound * 100f) / 100f;
6
CodeRank
This code example shows how to use the following methods:distanceTo, setLatitude, setLongitude
	public double getDistanceTravelled(double originLatitude,
			double originLongitude, double destinationLatitude,
			double destinationLongitude) {
		 
		// origin location 
		Location originLocation = new Location("");
		originLocation.setLatitude(originLatitude);
		originLocation.setLongitude(originLongitude);
 
		// destination location 
		Location destinationLocation = new Location("");
		destinationLocation.setLatitude(destinationLatitude);
		destinationLocation.setLongitude(destinationLongitude);
		 
		// calculate the distance between the two locations 
		float distance = originLocation.distanceTo(destinationLocation);
		 
		// round the float and return 
		// TODO might need more here.. test cases will find that out 
		//return Math.round(distance); 
		return distance;
6
CodeRank
This code example shows how to use the following methods:distanceTo, getAccuracy, getLatitude, getLongitude, getProvider
 
	@Override 
	public void onLocationChanged(Location location) {
		if(esMejorLocation(location)){
			ardbh.actualizaDistancia(idSesion, distancia(location));
			ardbh.insertarPosicion(idSesion, location.getLatitude(), location.getLongitude());
			 
			ultimaMejorLocation = location;
		} 
	} 
 
	@Override 
	public void onProviderDisabled(String provider) {
		activity.showDialog(0);
	} 
 
	@Override 
	public void onProviderEnabled(String provider) {
		 
	} 

6
CodeRank

Related Location distanceTo Questions & Answers:

distanceTo Method Overview
Returns the approximate distance in meters between this location and the given location.
See Code Examples for other Android Location Methods: