Reputation: 1446
I have this View Adapter method for a ListView which is populated from a JSON file.
One of the components I wanna display on a TextView of each list item is the distance between the user's location and each location which is displayed on the ListView.
But the app crashed everytime I run it, I know there's something wrong with my code starting from //calculate the distance
.
Can anybody help me to analyze and fix on the lines where I went wrong probably?
Here's the sample of the parsed longitude and latitude from the JSON:
"latitude": 52.5074779785632,
"longitude": 13.3903813322449,
and here's the method I'm using:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LocationsListActivity.this.getLayoutInflater().inflate(R.layout.listitems, null, true);
}
VideoLocation vidLocation = videoLocations[position];
ImageView v = (ImageView)convertView.findViewById(R.id.image);
String url = vidLocation.documentary_thumbnail_url;
v.setTag(url);
loader.DisplayImage(url, ctx, v);
TextView titleView = (TextView)convertView.findViewById(R.id.txt_title);
String title = vidLocation.name;
titleView.setText(title.toUpperCase());
TextView descView = (TextView)convertView.findViewById(R.id.txt_list_desc);
String desc = vidLocation.text;
descView.setText(desc);
//calculate the distance
Location newLocation = new Location("User");
GeoPoint geopoint = new GeoPoint(
(int) (newLocation.getLatitude() * 1E6), (int) (newLocation
.getLongitude() * 1E6));
GeoPoint myposition = geopoint;
Location locationA = new Location("point A");
Location locationB = new Location("point B");
locationA.setLatitude(geopoint.getLatitudeE6() / 1E6);
locationA.setLongitude(geopoint.getLongitudeE6() / 1E6);
locationB.setLatitude(vidLocation.latitude/ 1E6);
locationB.setLongitude(vidLocation.longitude / 1E6);
double distance = locationA.distanceTo(locationB);
String distances = String.valueOf(distance);
TextView distanceView = (TextView)convertView.findViewById(R.id.txt_distance);
System.out.println(distances);
distanceView.setText(distances+" m");
return convertView;
}
Upvotes: 0
Views: 700
Reputation: 39406
Location newLocation = new Location("User");
"User" is not a valid LocationProvider. It should at least be one of
LocationManager.getAllProviders();
(Usually "gps" or "network")
Also, in your code, newLocation is not really initialized. Its values are most likely empty. You should obtain the user location with something like, for instance:
LocationManager.getLastKnownLocation(null);
Upvotes: 1
Reputation: 36816
When your application crashes and you ask about it on StackOverflow, you should always include the logcat output of the crash details. The answer is almost always right there and very easy to pick out.
What you're doing here is using the Location object incorrectly. If you look at the docs for the constructor you're using, you'll see that this just returns a default location object and doesn't contain the devices actual location. Also, the string that you pass into this is not arbitrary, it's the name of the location provider to be associated with this Location object.
To get the user's last known location, get an instance of the LocationManager and call getLastKnownLocation. This also takes a string which corresponds to a location provider that should be used.
Read the docs on obtaining user location and look into the Criteria object and the LocationManager.getBestProvider method. These are the safest ways to get the best location and not crash in the process. For example, if you request location passing the GPS provider string and the device doesn't have a GPS or the user has their GPS turned off, your code will crash (I believe you get a null object from getLastKnownLocation in this case). Also make sure to add the appropriate permissions to the manifest.
http://developer.android.com/guide/topics/location/obtaining-user-location.html
http://developer.android.com/reference/android/location/Criteria.html
http://developer.android.com/reference/android/location/LocationManager.html
Upvotes: 1