Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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();
}
}
<?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>
<?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>