Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find distance using google places api

Tags:

android

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?

like image 289
user1701593 Avatar asked Oct 05 '22 02:10

user1701593


2 Answers

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

like image 173
Shridutt Kothari Avatar answered Oct 10 '22 02:10

Shridutt Kothari


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;
    }
}
like image 40
user1701593 Avatar answered Oct 10 '22 03:10

user1701593