1

I'm developing an Android app. I need to compare locations and see what the distance is between them. However, I have a lot of them. The easiest way I have found is to store them in a string-array, and then to compare them. I have searched, but I have not found a way to make an item from a string array into a location. I tried:

double distance

Location locationA = new Location(“point A”)

locationA.setLatitude(latitude);

locationA.setLongitude(Longitude);

Location locationB = new Location(“point B”);

locationB.setLatitude(Latitude.get(0));

LocationB.setLongitude(latitude.get(0));

distance = locationA.distanceTo(locationB);

But it does not work. My code is posted below:

NearestStations.java

public class Neareststations extends Activity implements LocationListener {
LocationManager mLocationManager;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.neareststations);
    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);



        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        Log.i("MYTAG", String.valueOf(longitude));
        Log.i("MYTAG", String.valueOf(latitude));
        if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
            // Do something with the recent location if it is less than two minutes old
            Log.i("MYTAG", String.valueOf(location));
            Log.i("MYTAG", String.valueOf(longitude));
            Log.i("MYTAG", String.valueOf(latitude));
        }
        else {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 50, this);
            Log.i("MYTAG", String.valueOf(location));
        }
    }

    public void onLocationChanged(Location location) {
        if (location != null) {
        // Do something withthe current location
            Log.i("MYTAG", String.valueOf(location));

        }
    }

    public void onProviderDisabled(String arg0) {}
    public void onProviderEnabled(String arg0) {}
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

    String[] Stations = getResources().getStringArray(R.array.Stations);
    String[] Longitude = getResources().getStringArray(R.array.Longitude);
    String[] Latitude = getResources().getStringArray(R.array.Latitude);

    Map<String, String> myMap = new HashMap<String, String>();{
    for (int i = 0; i <8; i++) {
        myMap.put(Stations[i], Latitude[i]);
    }
    }

    Map<String, String> myMap1 = new HashMap<String, String>();{
    for (int h = 0; h <8; h++) {
        myMap1.put(Stations[h], Longitude[h]);
    }
    }

How can I do this? My string-array's are standard and have entries such as

<item>-87.669147</item>
<item>-87.680622</item>

Thank you for your help.

4
  • But it does not work. means that you're getting zero or crashes or ... what? Also, I believe you have some typo as you're having LocationB.setLongitude(latitude.get(0));. At least from method naming shouldn't you provide a longitude? Commented Oct 9, 2013 at 21:45
  • What do you mean by doesn't work? Do you get the wrong location? Do you get an exception or a crash? Please post actual information so we can help oyu. Commented Oct 9, 2013 at 21:45
  • cant your just do Double.parse(latitude)? Commented Oct 9, 2013 at 21:46
  • @gunar I meant longitude. Commented Oct 9, 2013 at 21:48

2 Answers 2

5

The strings need to be converted into doubles. Also, you should be using distanceBetween() when comparing two points but distanceTo() will work fine for smaller distances.

Here's a basic example of converting String values to doubles for use with Location. Distance will be returned in meters, so multiply by 0.000621371192237334 if you want miles.

double distance;  

String str_lat_start = "29.287260";
String str_lon_start = "-81.100327";
String str_lat_end = "26.016045";
String str_lon_end = "-80.150169";

double lat_start = 0;
double lon_start = 0;
double lat_end = 0;
double lon_end = 0;

try {

    lat_start = Double.parseDouble( str_lat_start ); 
    lon_start = Double.parseDouble( str_lon_start );
    lat_end = Double.parseDouble( str_lat_end );
    lon_end = Double.parseDouble( str_lon_end );

} catch (NumberFormatException e) {
    Log.v("Main", "Convert to Double Failed : ");
}

Location locationA = new Location("point A");  
locationA.setLatitude( lat_start );  
locationA.setLongitude( lon_start );  

Location locationB = new Location("point B");  
locationB.setLatitude( lat_end );  
locationB.setLongitude( lon_end );  

distance = locationA.distanceTo(locationB) * 0.000621371192237334; 
Sign up to request clarification or add additional context in comments.

5 Comments

Correct me if I'm wrong, but isn't distanceBetween() only used for long distances?
Also, can you post some code for the converting part? I get The method convert(String) is undefined for the type Double when I try Double G; G.convert(Lon);
Updated with a code example for converting string to double. You'll want to perform conversion in a try block and catch exceptions, otherwise your app will crash.
Also, if you're using LocationManager, you'll want to add these permissions to your Manifest..... <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Thanks, that looks great. Is there an easy way to iterate this or repeat it, because I have a lot of these I need to do.
1
float[] results = new float[3];
    Location.distanceBetween(locationA.getLatitude(), locationA.getLongitude(), locationB.getLatitude(), locationB.getLongitude(), results);

Distance between A and B is results[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.