Display distance between 2 locations on a ListView
<p>I have this View Adapter method for a ListView which is populated from a JSON file. </p>

<p>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.</p>

<p>But the app crashed everytime I run it, I know there's something wrong with my code starting from <code>//calculate the distance</code>.</p>

<p>Can anybody help me to analyze and fix on the lines where I went wrong probably?</p>

<p>Here's the sample of the parsed longitude and latitude from the JSON:</p>

<pre><code> "latitude": 52.5074779785632,
 "longitude": 13.3903813322449,
</code></pre>

<p>and here's the method I'm using:</p>

<pre><code>@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;
        }
</code></pre>
