2

This is my first time development in android application. This app will calculate the total distance between two address that input by the user in order to get the total distance. Is it ok to calculate the distance that assume the road is straight? by the way, this application will not show map and does not need GPS! It just needs to use geocoder to get the latitude-longitude.

Here are my questions:

  1. what would the AndroidManifest.xml would look like? do i need to include android:name=".permission.MAPS_RECEIVE".
  2. In the main_activity.java, should i use activity or use fragment activity? public class MainActivity extends Activity

Thanks for your reply and i would like to appreciate u guys and if possible can you please show the full AndroidManifext.xml code?

Thanks.

TheFlash
  • 5,997
  • 4
  • 41
  • 46
user2949434
  • 35
  • 1
  • 3

3 Answers3

2

http://developer.android.com/reference/android/location/Location.html

Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:

Location locationA = new Location("point A");

locationA.setLatitude(latA);
locationA.setLongitude(lngA);

Location locationB = new Location("point B");

locationB.setLatitude(latB);
locationB.setLongitude(lngB);

float distance = locationA.distanceTo(locationB);

or

private double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) {
    float pk = (float) (180/3.14169);

    float a1 = lat_a / pk;
    float a2 = lng_a / pk;
    float b1 = lat_b / pk;
    float b2 = lng_b / pk;

    float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*FloatMath.cos(b1)*FloatMath.cos(b2);
    float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*FloatMath.cos(b1)*FloatMath.sin(b2);
    float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
    double tt = Math.acos(t1 + t2 + t3);

    return 6366000*tt;
}
Sonu Singh Bhati
  • 1,093
  • 10
  • 11
1

Use the Geocoder to get the latitude and longitude for place 1 and place 2. Apply those values in the following method.

Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results)
1

You don't need to include the maps permissions.

To get the distance between 2 locations you can use the Geocoder class. This shouldn't need an y location permissions either.

The Geocoder class has a method

getFromLocationName(String locationName, int maxResults)

which returns a list of Addresses for a location input. The addresses have a latitude/longitude.

You need to convert the Address into a Location object which has methods for calculating distance between 2 locations:

distanceTo(Location dest)
Returns the approximate distance in meters between this location and the given location.

As for your second question. You should use Fragments inside of your activity. Have a read through this to understand a bit more about fragments and their advantages.

athor
  • 6,848
  • 2
  • 34
  • 37