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.
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 havingLocationB.setLongitude(latitude.get(0));. At least from method naming shouldn't you provide a longitude?Double.parse(latitude)?