Get TIME between two geopoints

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Get TIME between two geopoints

Postby zimonomiz » Mon Mar 07, 2011 9:59 am

Hey

Is there any way to get the driving time between to geopoints? I want to create an algorithm that calculates the time from the users location to several destinations and then chooses the shortest (by time).
zimonomiz
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 19, 2011 9:47 pm

Top

Re: Get TIME between two geopoints

Postby zimonomiz » Mon Mar 14, 2011 10:30 am

Does no one have an idea?
zimonomiz
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 19, 2011 9:47 pm

Re: Get TIME between two geopoints

Postby ruelblapid » Mon Mar 14, 2011 1:53 pm

hi Zimonomiz,

I think your question has been answered here
distance_calculation_between_2_geopoint_by_haversine_formula-t3062.html

Best,
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: Get TIME between two geopoints

Postby zimonomiz » Mon Mar 14, 2011 2:46 pm

Hey ruelblapid

no that is different. What I need is the travel time you need to drive from one point to another. The thread you send just offers the direct distance between two points in meters etc.

Is there no way to get the time instead?
zimonomiz
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 19, 2011 9:47 pm

Re: Get TIME between two geopoints

Postby ruelblapid » Mon Mar 14, 2011 3:06 pm

Hi zimonomiz,

The basic idea is in there what you're just going to do is use the Distance Formula we need a little bit of physics here to get the time.

D = distance
S = speed
T = time

T = D/V

refer to this link for more information about distance formula
http://www.idealmath.com/algebra/equati ... prob01.htm

D = the distance between GeoPoint
V = assuming the end user set this or you have default value
Note: this will give the time in decimal hours. To get the decimal part in minutes, you have to multiply the decimal part by 60.

EG: D = 97 mi; V = 39 mi/hr;
T = 97/39
T = 2.4872 hr; .4872*60 = 29.2 min
T = 2 hr 29.2 min

Best,
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: Get TIME between two geopoints

Postby zimonomiz » Mon Mar 14, 2011 4:13 pm

Hey

I understand that, but it is still not what I need. The driving time between two geopoints (Route time) is not just the distance divided by the average speed. As you might know the direct distance is way lower than the actual driving distance. The other problem is, that the average speed varies, depending on the streets you use (highways etc.). This makes it very complex to calculate the time by myself. Your method works but is no precise enough for what I have in mind.

Is there no way to get the driving time calculated by google maps. That would be precise enough
zimonomiz
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 19, 2011 9:47 pm

Top

Re: Get TIME between two geopoints

Postby ruelblapid » Wed Mar 16, 2011 7:18 am

Hi Zimonomiz,

Is there no way to get the driving time calculated by google maps. That would be precise enough


Here's another solution see code snippet below:
Example
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                String origin = "41.8756400,-87.6422300"; //lon and lat of the starting point
  2.                 String destination = "41.8690300,-87.6449900"; //lon and lat of the endpoint
  3.                 String mode = "walking"; //driving,walking
  4.                 String url = String.format("http://maps.googleapis.com/maps/api/directions/json?origin=%s&destination=%s&mode=%s&sensor=false", origin,destination,mode);
  5.                 String json = getDirection(url);
  6.  
  7.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private  String getDirection(String strURL)
  2.     {
  3.                 StringBuffer sb = new StringBuffer();
  4.         try {
  5.                         URL url = new URL(strURL);
  6.                         HttpURLConnection connection = (HttpURLConnection)url.openConnection();                
  7.                         connection.setRequestMethod("GET");
  8.                         connection.setDoOutput(true);
  9.                         connection.setDoInput(true);
  10.                         connection.connect();
  11.                        
  12.                         int responseCode = connection.getResponseCode();
  13.                        
  14.                         if(responseCode == HttpURLConnection.HTTP_OK) {
  15.                                
  16.                                 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  17.                                 String line;
  18.                                
  19.                                 while((line = reader.readLine()) != null)
  20.                                         sb.append(line);
  21.                                
  22.                         }
  23.                        
  24.                 }
  25.                 catch(MalformedURLException mex) {             
  26.                 }
  27.                 catch(IOException iex) {                       
  28.                 }              
  29.                 return sb.toString();
  30.     }
  31.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

for more info check this link http://code.google.com/intl/lv/apis/maps/documentation/directions/
The return value is json, You can use JSONObject for parsing. Hope this one help.

Best,
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: Get TIME between two geopoints

Postby zimonomiz » Thu Apr 21, 2011 11:25 am

Hey guys

So I finally got what I wanted, I wrote an app that can give you the time and distance between two addresses. The good thing is that you get the real driving distance in meters and not beeline as in distancebetween(). Also the time is the real time you get from google maps. Changing the TravelMode to Walking is also possible but not implemented yet. If there are any questions let me know.

MainActivity.java:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MainActivity extends Activity {
  2.        
  3.  
  4.         private double fromLat;
  5.         private double fromLon;
  6.         private double toLat;
  7.         private double toLon;
  8.         private int timeToDestination;
  9.         private int distanceToDestination;
  10.  
  11.         JSONObject json_data_invitation = new JSONObject();
  12.  
  13.         JSONArray jArray_invitation;
  14.  
  15.     /** Called when the activity is first created. */
  16.     @Override
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.main);
  20.        
  21.        
  22.         final EditText start = (EditText)findViewById(R.id.fromAdress);
  23.         final EditText destination = (EditText)findViewById(R.id.toAdress);
  24.  
  25.         final TextView kml = (TextView)findViewById(R.id.kml_text);
  26.         Button calculate = (Button)findViewById(R.id.calculate);
  27.        
  28.         calculate.setOnClickListener(new View.OnClickListener() {
  29.                        
  30.                         public void onClick(View view) {
  31.  
  32.                                
  33.                                
  34.                                 String url = getUrl(start.getText().toString(), destination.getText().toString() );
  35.  
  36.                                
  37.                                
  38.                                 String result = "";
  39.                                 InputStream is = null;
  40.  
  41.                        
  42.                         try{
  43.                                 HttpClient httpclient = new DefaultHttpClient();
  44.                                 HttpPost httppost = new HttpPost(url);
  45.                                 HttpResponse response = httpclient.execute(httppost);
  46.                                 HttpEntity entity = response.getEntity();
  47.                                 is = entity.getContent();              
  48.                         } catch(Exception e) {
  49.                                 Log.e("log_tag", "Error in http conection"+e.toString());}
  50.                                
  51.  
  52.  
  53.                         try {
  54.                                 BufferedReader reader = new BufferedReader(
  55.                                 new InputStreamReader(is,"iso-8859-1"),8);
  56.                                 StringBuilder sb = new StringBuilder();
  57.                                 String line = null;
  58.                                 while ( (line = reader.readLine() ) != null) {
  59.                                 sb.append(line + "\n"); }
  60.                                 is.close();
  61.                                 result=sb.toString();
  62.  
  63.                         }catch(Exception e){
  64.                                 Log.e("log_tag", "Error converting result "+e.toString());
  65.                         }
  66.                        
  67.  
  68.                                 try {
  69.                                         JSONObject rootObj = new JSONObject(result); //rootObj ist jetzt ein dict
  70.                                         JSONArray routes = (JSONArray) rootObj.get("routes");
  71.                                         if(routes.length()<1)
  72.                                                 kml.setText("ERROR no route there");
  73.                                         JSONObject firstRoute = routes.getJSONObject(0);
  74.                                         JSONArray legs = (JSONArray) firstRoute.get("legs");
  75.                                         if(legs.length()<1)
  76.                                                 kml.setText("ERROR no legs there");
  77.                                         JSONObject firstLeg = legs.getJSONObject(0);
  78.                                         JSONObject durationObject = (JSONObject) firstLeg.get("duration");
  79.                                         JSONObject distanceObject = (JSONObject) firstLeg.get("distance");
  80.                                         // finally we will get the values distance in meters and time in seconds!!
  81.  
  82.                                         timeToDestination = (Integer) durationObject.get("value");
  83.                                         distanceToDestination = (Integer) distanceObject.get("value");
  84.                                        
  85.                                         kml.setText("Time: "+timeToDestination+" "+"Meters: "+distanceToDestination);
  86.                                        
  87.                                 } catch (JSONException e) {
  88.                                         // TODO Auto-generated catch block
  89.                                         e.printStackTrace();
  90.                                 }
  91.  
  92.                                
  93.                                
  94.                                
  95.  
  96.                                
  97.                                
  98.                                
  99.                         }
  100.                 });
  101.                
  102.        
  103.     }
  104.    
  105.     // String Builder for Google Maps JSON will create the Link for the google maps query based on the entered data
  106.    
  107.     public static String getUrl(String fromAdress, String toAdress) {// connect to map web service
  108.     StringBuffer urlString = new StringBuffer();
  109.     urlString.append("http://maps.google.com/maps/api/directions/json?origin=");
  110.     urlString.append(fromAdress.toString());
  111.     urlString.append("&destination=");
  112.     urlString.append(toAdress.toString());
  113.     urlString.append("&sensor=false");
  114.     return urlString.toString();
  115. }
  116.  
  117. }
Parsed in 0.049 seconds, using GeSHi 1.0.8.4


Main.xml:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:background="#FFFFFF">
  7.    
  8.     <EditText
  9.         android:id="@+id/fromAdress"
  10.         android:layout_width="180dp"
  11.             android:layout_height="wrap_content"
  12.             android:layout_marginTop="20dp"
  13.             android:text="Munich"></EditText>
  14.     <EditText
  15.         android:layout_width="180dp"
  16.             android:layout_height="wrap_content"
  17.             android:layout_marginTop="20dp"
  18.             android:text="Garching"
  19.             android:id="@+id/toAdress"></EditText>
  20.         <LinearLayout
  21.                 android:orientation="vertical"
  22.                 android:layout_width="fill_parent"
  23.                 android:layout_height="270dp"
  24.                 android:background="#ffffff">
  25.  
  26.         <ScrollView android:layout_width="fill_parent"
  27.                                 android:layout_height="wrap_content">  
  28.        
  29.        
  30.        
  31.                 <RelativeLayout
  32.                          android:orientation="vertical"
  33.                          android:layout_width="fill_parent"
  34.                          android:layout_height="335dp"
  35.                          android:background="#ffffff">
  36.                          
  37.  
  38.     <TextView
  39.         android:layout_width="wrap_content"
  40.             android:layout_height="wrap_content"
  41.             android:layout_marginTop="20dp"
  42.             android:id="@+id/kml_text"
  43.             android:textColor="#000000"></TextView>
  44.            
  45.                 </RelativeLayout>
  46.         </ScrollView>
  47.         </LinearLayout>
  48.         <LinearLayout
  49.                 android:orientation="vertical"
  50.                 android:layout_width="fill_parent"
  51.                 android:layout_height="fill_parent"
  52.                 android:background="#ffffff">            
  53.                
  54.                 <RelativeLayout
  55.                         android:orientation="vertical"
  56.                         android:layout_width="fill_parent"
  57.                         android:layout_height="fill_parent"
  58.                         android:background="#ffffff">  
  59.     <Button
  60.         android:layout_width="wrap_content"
  61.             android:layout_height="wrap_content"
  62.             android:layout_alignParentBottom="true"
  63.             android:layout_alignParentRight="true"
  64.             android:id="@+id/calculate"
  65.             android:text="calculate"></Button>
  66.            
  67.                         </RelativeLayout>
  68.         </LinearLayout>          
  69.  
  70. </LinearLayout>
  71.  
Parsed in 0.046 seconds, using GeSHi 1.0.8.4



And last but not least the AndroidManifest.xml:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.       package="com.example"
  4.       android:versionCode="1"
  5.       android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".Eingabe"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.  
  15.     </application>
  16.  
  17. <uses-permission android:name="android.permission.INTERNET" />
  18.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  19.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  20.  
  21.  
  22. </manifest>
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


I hope this works out for you as it did for me!
zimonomiz
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 19, 2011 9:47 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 5 guests