• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Get TIME between two geopoints

zimonomiz

Lurker
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).
 
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:

Code:
public class MainActivity extends Activity {
	

	private double fromLat;
	private double fromLon;
	private double toLat;
	private double toLon;
	private int timeToDestination;
	private int distanceToDestination;

	JSONObject json_data_invitation = new JSONObject();

	JSONArray jArray_invitation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        final EditText start = (EditText)findViewById(R.id.fromAdress);
        final EditText destination = (EditText)findViewById(R.id.toAdress);

        final TextView kml = (TextView)findViewById(R.id.kml_text);
        Button calculate = (Button)findViewById(R.id.calculate);
        
        calculate.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View view) {

				
				
				String url = getUrl(start.getText().toString(), destination.getText().toString() );

				
				
				String result = "";
				InputStream is = null;

	        	
	        	try{
	        		HttpClient httpclient = new DefaultHttpClient();
	        		HttpPost httppost = new HttpPost(url);
	        		HttpResponse response = httpclient.execute(httppost);
	        		HttpEntity entity = response.getEntity();
	        		is = entity.getContent();        	
	        	} catch(Exception e) {
	        		Log.e("log_tag", "Error in http conection"+e.toString());}
	        		


	        	try {
	        		BufferedReader reader = new BufferedReader(
	        		new InputStreamReader(is,"iso-8859-1"),8);
	        		StringBuilder sb = new StringBuilder();
	        		String line = null;
	        		while ( (line = reader.readLine() ) != null) {
	        		sb.append(line + "\n"); }
	        		is.close();
	        		result=sb.toString();

	        	}catch(Exception e){
	        		Log.e("log_tag", "Error converting result "+e.toString());
	        	}
	        	

				try {
					JSONObject rootObj = new JSONObject(result); //rootObj ist jetzt ein dict
					JSONArray routes = (JSONArray) rootObj.get("routes");
					if(routes.length()<1)
						kml.setText("ERROR no route there");
					JSONObject firstRoute = routes.getJSONObject(0);
					JSONArray legs = (JSONArray) firstRoute.get("legs");
					if(legs.length()<1)
						kml.setText("ERROR no legs there");
					JSONObject firstLeg = legs.getJSONObject(0);
					JSONObject durationObject = (JSONObject) firstLeg.get("duration");
					JSONObject distanceObject = (JSONObject) firstLeg.get("distance");
					// finally we will get the values distance in meters and time in seconds!!

					timeToDestination = (Integer) durationObject.get("value");
					distanceToDestination = (Integer) distanceObject.get("value");
					
					kml.setText("Time: "+timeToDestination+" "+"Meters: "+distanceToDestination);
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				
				
				

				
				
				
			}
		});
		
        
    }
    
    // String Builder for Google Maps JSON will create the Link for the google maps query based on the entered data
    
    public static String getUrl(String fromAdress, String toAdress) {// connect to map web service
    StringBuffer urlString = new StringBuffer();
    urlString.append("http://maps.google.com/maps/api/directions/json?origin=");
    urlString.append(fromAdress.toString());
    urlString.append("&destination=");
    urlString.append(toAdress.toString());
    urlString.append("&sensor=false");
    return urlString.toString();
}

}

Main.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF">
    
    <EditText
    	android:id="@+id/fromAdress"
    	android:layout_width="180dp"
   	    android:layout_height="wrap_content"
   	    android:layout_marginTop="20dp"
   	    android:text="Munich"></EditText>
    <EditText
    	android:layout_width="180dp"
   	    android:layout_height="wrap_content"
   	    android:layout_marginTop="20dp"
   	    android:text="Garching"
   	    android:id="@+id/toAdress"></EditText>
	<LinearLayout
		android:orientation="vertical"
		android:layout_width="fill_parent" 
		android:layout_height="270dp"
		android:background="#ffffff">

	<ScrollView android:layout_width="fill_parent" 
				android:layout_height="wrap_content">	
	
	
	
		<RelativeLayout
		         android:orientation="vertical"
		         android:layout_width="fill_parent" 
		         android:layout_height="335dp"
		         android:background="#ffffff">
		         

    <TextView
    	android:layout_width="wrap_content"
   	    android:layout_height="wrap_content"
   	    android:layout_marginTop="20dp"
   	    android:id="@+id/kml_text"
   	    android:textColor="#000000"></TextView>
   	    
   		</RelativeLayout>
	</ScrollView>
	</LinearLayout>
	<LinearLayout
		android:orientation="vertical"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		android:background="#ffffff">		 
		
		<RelativeLayout
			android:orientation="vertical"
			android:layout_width="fill_parent" 
			android:layout_height="fill_parent"
			android:background="#ffffff">	
    <Button
    	android:layout_width="wrap_content"
   	    android:layout_height="wrap_content"
   	    android:layout_alignParentBottom="true"
   	    android:layout_alignParentRight="true"
   	    android:id="@+id/calculate"
   	    android:text="calculate"></Button>
   	    
   	 		</RelativeLayout>
	</LinearLayout>		 

</LinearLayout>


And last but not least the AndroidManifest.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Eingabe"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


</manifest>
]

I hope this works out for you as it did for me!
 
Back
Top Bottom