5

I am developing an app using Google pLaces api. i am able to get the list of cafe that are near a user who is using my app. the resultant list includes details of places such as the name of the place, its address, latitude longitude values and phone number. What i want to know is how do i request place api to return the distance between the user and each of the entry in the result list. i have gone through few links posted on stackoverflow that discuss a similar topic. i went through Googles documentation as well, but it does not mention anything about getting these distances. i want to refrain myself from using any mathematical formula like great circle algorithm because in that case the distance returned will be straight line distance and that will not be an accurate estimation of distance. so can anybody please tell me how do request places api to give me the distance between the user and every entry in the result?

few of the links that i went through are: How do i find distance between one place to another using Geolocation or Similiar API without embedding a Google Map?

find Distance in kilometers in Android using google places Api

How to get Distance Kilometer in android?

Community
  • 1
  • 1
user1701593
  • 461
  • 1
  • 9
  • 15

5 Answers5

5

I Think As you said- you want to refrain yourself from using

any mathematical formula so you can Also try this-

You can get distance from address of location

http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+a+"&destinations="+b+"&mode=driving&language=en-EN&sensor=false with a=addressFrom, b=addressTo

If you have lat,long of location, you can get address of that location as:

 double latiTude = 10.3929393;
 double longiTude = 150.93984884;

 Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
  GeoPoint p= new GeoPoint((int) (latiTude * 1E6), (int) (longiTude * 1E6));
  List<Address> address = geocoder.getFromLocation(
                                                    p.getLatitudeE6() / 1E6,
                                                    p.getLongitudeE6() / 1E6, 1);
    if (address.size() > 0) 
    {
    String addressFroLatLong= "";
    for (int i = 0; i < address.get(0).getMaxAddressLineIndex(); i++) 
    {   addressFroLatLong+= address.get(0).getAddressLine(i) + "\n";
    }   
   }

PLease upvote if helped. nothing else. Thanks, happy to help enjoy..

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
  • 1
    Hi Shridutt, thank u for your kind reply! I appreciate it! the link that you have provided, http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+a+"&destinations="+b+"&mode=driving&language=en-EN&sensor=false with a=addressFrom, b=addressT does it accept latitude longitude values? my app will have 2 set of values. one lat lon value that corresponding to users location and other lat lon value corresponding to all the listings in the result set. using this url will help me get distance? also is this service free to use? please reply – user1701593 Feb 10 '13 at 17:55
  • @user1701593: You need to do the further r&d on this as currently i am not working on this but i had worked on this a long time before so posted the code snippets... sorry for that But pls upvote if i helped...thanks. – Shridutt Kothari Feb 10 '13 at 18:02
3

Haversine implementation is given below. However i still feel that there should be some service offered by google places api that will help in finding the exact distance between two points. if anybody knows that then pl let me know.. as of now Haversine is a good substitute..

import java.lang.*;
import java.io.*;
import java.math.*;
public class DistanceCalculation {

public static void main(String[] args)
{
    double lon11 =-97.116121;
      double lat11=32.734642;

      double lon22=-97.111658;
      double lat22=32.731918;

    //   calculateDistance(lat11,lon11, lat22, lon22);
      distFrom(lat11, lon11, lat22, lon22);

}
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);
    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
            * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;
    System.out.println(dist);
    return dist;
    }
}
user1701593
  • 461
  • 1
  • 9
  • 15
0

The distance between two points can be found by using the equation described here: http://williams.best.vwh.net/avform.htm#Dist remember, use radians, not degrees!

It seems you have concern about updating a maps key in each release? I have an app in the market that is map-based, and you get 1 prod key and 1 debug key (I believe it's per developer, but if not, it's per app). It has to do with the key you use to sign your apk. What I'm saying is that if you get your prod api key, and only ever develop this one app, you can publish many many updates without ever having to touch the key again (assuming you don't do debug-key testing locally). If you're concerned about it, you can keep both in your AndroidManifext.xml file and just comment out the irrelevant one, depending on whether you're deploying to a device or emulator.

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
-1

The calculation between two geographical positions is not difficult, especially for your tasks, to find the distance to the hotels nearby.

you can use the haversine formula, that perfectly suited for your task.

But these geographic formulas give the air-distance, not the road distance, for which you would need a routing service.

raceworm
  • 196
  • 7
-2

Another Solution: You could also just do this:

//your two locations
Location locationA;
Location locationB;

//distance between them
float distanceBetweenPoints = locationA.distanceTo(locationB);

bit late but hopefully this helps :)

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61