1

How to get miles/km and minus from current location to latitude/longitude destination in google maps API V2. If i get this value I want to store in textview.

My code is:

import android.os.Bundle;

import com.actionbarsherlock.app.SherlockActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class DetailMapActivity extends SherlockActivity {
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_detail_map);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        map.setMyLocationEnabled(true);

        LatLng LOC_DESTINATION = new LatLng(-6.33438, 106.74316);

        map.addMarker(new MarkerOptions().position(LOC_DESTINATION)
                .title("Destination Title").snippet("Destination Description"));

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(LOC_DESTINATION, 40));

        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

        String miles = "what this code?";
        Log.d("HaichalMap", miles);
    }
}

Thanks for answer.

Dave Jackson
  • 837
  • 5
  • 19
  • 28

3 Answers3

2

Try this using Location Class

Location locationA = new Location("Location A");
locationA.setLatitude(lat_a);
locationA.setLongitude(lon_a);

Location locationB = new Location("Location B");
locationB.setLatitude(lat_b);
locationB.setLongitude(lon_b);

int metres = Math.round(locationA.distanceTo(locationB)); // Returns in Metres

Convert metres into desired output

moDev
  • 5,248
  • 4
  • 33
  • 63
0

Google Maps does not offer directions. You need to look at the Google Directions API

iagreen
  • 31,470
  • 8
  • 76
  • 90
0

You would need to calculate the distance depending upon your old and new co-ordinates (in latitudes and longitudes). This will give you back the result in meters. You then need to convert it into miles/km. Any how THIS thread maybe of help to you as far as the calculation of distance from initial and final co-ordinates are concerned. Hope that answered your question.

Community
  • 1
  • 1
Ricky
  • 81
  • 11