location distance problems

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

location distance problems

Postby g123456 » Sun Mar 20, 2011 8:21 pm

Greetings, i am developing an app to count distance in between 2 locations and if for example its smaller than 10 meters then vibrate or anything, I have tried proximity alert, done several things with my understanding on location managers. But all got a specific problem, distance output format and values in general. After trying distanceTo, distanceBetween and so on and everything giving my totally weird numbers and the worst of it while testing my app on Emulator i get more logical numbers( testing it with real location points around my house ), but when i put it on my android HTC and try to see it in reality how it works, it gives some totally weird numbers which possibly means that the input format from the HTC GPS service is giving the coords in different format? This is troubling me for some weeks because its not entirely a logical problem, it has to do with external factors.


Code: Select all

package com.test.test;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class TESTGPS extends Activity {

   private LocationManager lm;
   private LocationListener ll;

   private static Location loc;
   private Dialog dialog;

   private static TextView coordinates;

   public static double EARTH_RADIUS_KM = 6384; // km

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      coordinates = new TextView(this);
      setContentView(coordinates);
      

      
      
      lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      ll = new MyLocationListener();
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, ll);

      Runnable showWaitDialog = new Runnable() {

         @Override
         public void run() {
            while (loc == null) {
            }
            dialog.dismiss();

         }
      };

      dialog = ProgressDialog.show(TESTGPS.this, "Please wait...", "Retrieving GPS data ...", true);

      Thread t = new Thread(showWaitDialog);
      t.start();
   }
   

   private static class MyLocationListener implements LocationListener {

      

      //find distance


      public double calculateDistanceMeters(double aLong, double aLat, double bLong, double bLat)
      {

            double d2r = (Math.PI / 180);

            double dLat = (bLat - aLat) * d2r;
            double dLon = (bLong - aLong) * d2r;
            double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                  + Math.cos(aLat * d2r) * Math.cos(bLat * d2r)
                  * Math.sin(dLon / 2) * Math.sin(dLon / 2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

            return EARTH_RADIUS_KM * c * 1000;

         }

      
      @Override
      public void onLocationChanged(Location location) {

         
         Location locationTwo = new Location("test location");

         
         locationTwo.setLongitude(37.991729);
         locationTwo.setLatitude(-92.093993);
            
         
         // debugging
         if (location != null) {
            loc = location;
            coordinates.setText("GPS( Lat: " + loc.getLongitude() + " Long: " + loc.getLatitude() + ")                              "   +     
            "USer( Lat: " + locationTwo.getLongitude() + " Long: " + locationTwo.getLatitude() + ")" +
            "             dist: " +  calculateDistanceMeters(loc.getLongitude(), loc.getLatitude(), locationTwo.getLongitude(), locationTwo.getLatitude()));
            
         }
         
         /*
         if (location != null) {
            loc = location;
            coordinates.setText("Lat: " + loc.getLatitude() + " Long: "
                  + loc.getLongitude());
            
         }
         */
         
      
         if (  calculateDistanceMeters(loc.getLongitude(), loc.getLatitude(), locationTwo.getLongitude(), locationTwo.getLatitude()) < 10f )
         {
            coordinates.setText("You are close to your target"
             +
            "                                                "
            +
            
            "GPS( Lat: " + loc.getLongitude() + " Long: " + loc.getLatitude() +
            " )                                                            "   +     
            "USer( Lat: " + locationTwo.getLongitude() + " Long: " + locationTwo.getLatitude() + ")" +
            "             dist: " +  calculateDistanceMeters(loc.getLongitude(), loc.getLatitude(), locationTwo.getLongitude(), locationTwo.getLatitude()));
                        
               
         }
         
                  
         
      }
      
      @Override
      public void onProviderDisabled(String provider) {

      }

      @Override
      public void onProviderEnabled(String provider) {

      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {

      }

   }
      
}
g123456
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Mar 20, 2011 8:08 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests