Java tutorial
package com.example.b1013029.myapplication; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationManager; import android.net.Uri; import android.os.AsyncTask; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.maps.CameraUpdate; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.PolylineOptions; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class MapsActivity extends FragmentActivity { private GoogleMap gMap; // Might be null if Google Play services APK is not available. private static final int MENU_A = 0; private static final int MENU_B = 1; private static final int MENU_C = 2; public static String posinfo = ""; public static String info_A = ""; public static String info_B = ""; ArrayList<LatLng> markerPoints; public static MarkerOptions options; public ProgressDialog progressDialog; public String travelMode = "driving";//default @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // progressDialog = new ProgressDialog(this); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setMessage("??......"); progressDialog.hide(); //? markerPoints = new ArrayList<LatLng>(); final SupportMapFragment mapfragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); gMap = mapfragment.getMap(); //?? LatLng location = new LatLng(34.802556297454004, 135.53884506225586); gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 17)); if (gMap != null) { gMap.setMyLocationEnabled(true); // gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { @Override public void onMapClick(LatLng point) { //3?? if (markerPoints.size() > 1) { markerPoints.clear(); gMap.clear(); } markerPoints.add(point); options = new MarkerOptions(); options.position(point); if (markerPoints.size() == 1) { options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)); options.title("A"); } else if (markerPoints.size() == 2) { options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); options.title("B"); } gMap.addMarker(options); gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { // TODO Auto-generated method stub String title = marker.getTitle(); if (title.equals("A")) { marker.setSnippet(info_A); } else if (title.equals("B")) { marker.setSnippet(info_B); } return false; } }); if (markerPoints.size() == 2) { // routeSearch(); } } }); } setUpMapIfNeeded(); initMap(); } private void routeSearch() { progressDialog.show(); LatLng origin = markerPoints.get(0); LatLng dest = markerPoints.get(1); String url = getDirectionsUrl(origin, dest); DownloadTask downloadTask = new DownloadTask(); downloadTask.execute(url); } private String getDirectionsUrl(LatLng origin, LatLng dest) { String str_origin = "origin=" + origin.latitude + "," + origin.longitude; String str_dest = "destination=" + dest.latitude + "," + dest.longitude; String sensor = "sensor=false"; // String parameters = str_origin + "&" + str_dest + "&" + sensor + "&language=ja" + "&mode=" + travelMode; //JSON String output = "json"; String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; return url; } private String downloadUrl(String strUrl) throws IOException { String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try { URL url = new URL(strUrl); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } data = sb.toString(); br.close(); } catch (Exception e) { Log.d("Exception", e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } return data; } private class DownloadTask extends AsyncTask<String, Void, String> { //???? @Override protected String doInBackground(String... url) { String data = ""; try { // Fetching the data from web service data = downloadUrl(url[0]); } catch (Exception e) { Log.d("Background Task", e.toString()); } return data; } // doInBackground() @Override protected void onPostExecute(String result) { super.onPostExecute(result); ParserTask parserTask = new ParserTask(); parserTask.execute(result); } } /*parse the Google Places in JSON format */ private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { @Override protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) { JSONObject jObject; List<List<HashMap<String, String>>> routes = null; try { jObject = new JSONObject(jsonData[0]); parseJsonpOfDirectionAPI parser = new parseJsonpOfDirectionAPI(); routes = parser.parse(jObject); } catch (Exception e) { e.printStackTrace(); } return routes; } //???? @Override protected void onPostExecute(List<List<HashMap<String, String>>> result) { ArrayList<LatLng> points = null; PolylineOptions lineOptions = null; MarkerOptions markerOptions = new MarkerOptions(); if (result.size() != 0) { for (int i = 0; i < result.size(); i++) { points = new ArrayList<LatLng>(); lineOptions = new PolylineOptions(); List<HashMap<String, String>> path = result.get(i); for (int j = 0; j < path.size(); j++) { HashMap<String, String> point = path.get(j); double lat = Double.parseDouble(point.get("lat")); double lng = Double.parseDouble(point.get("lng")); LatLng position = new LatLng(lat, lng); points.add(position); } //? lineOptions.addAll(points); lineOptions.width(10); lineOptions.color(0x550000ff); } //?? gMap.addPolyline(lineOptions); } else { gMap.clear(); Toast.makeText(MapsActivity.this, "?????????", Toast.LENGTH_LONG).show(); } progressDialog.hide(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //getMenuInflater().inflate(R.menu.main, menu); menu.add(0, MENU_A, 0, "Info"); menu.add(0, MENU_B, 0, "Legal Notices"); menu.add(0, MENU_C, 0, "Mode"); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_A: //show_mapInfo(); return true; case MENU_B: //Legal Notices(?) String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext()); AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MapsActivity.this); LicenseDialog.setTitle("Legal Notices"); LicenseDialog.setMessage(LicenseInfo); LicenseDialog.show(); return true; case MENU_C: //show_settings(); return true; } return false; } // private void re_routeSearch() { progressDialog.show(); LatLng origin = markerPoints.get(0); LatLng dest = markerPoints.get(1); // gMap.clear(); // //A options = new MarkerOptions(); options.position(origin); options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)); options.title("A"); options.draggable(true); gMap.addMarker(options); //B options = new MarkerOptions(); options.position(dest); options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); options.title("B"); options.draggable(true); gMap.addMarker(options); String url = getDirectionsUrl(origin, dest); DownloadTask downloadTask = new DownloadTask(); downloadTask.execute(url); } private void initMap() { this.gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); } public void onClickButton(View view) { setMyLocation(); } public void onClickButton2(View view) { test1(); } private void setMyLocation() { LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); Location lastLocate = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (lastLocate != null) { LatLng position = new LatLng(lastLocate.getLatitude(), lastLocate.getLongitude()); this.gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, gMap.getCameraPosition().zoom)); } else { Toast.makeText(this, "????????", Toast.LENGTH_SHORT).show(); } } // ??? private void test0() { String start = "?"; String destination = ""; // :r //String dir = "r"; // :d //String dir = "d"; // ??:w String dir = "w"; Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); intent.setData(Uri .parse("http://maps.google.com/maps?saddr=" + start + "&daddr=" + destination + "&dirflg=" + dir)); startActivity(intent); } private void test1() { String src_lat = "41.841817"; String src_ltg = "140.766969"; String des_lat = "41.830257"; String des_ltg = "140.761644"; Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); intent.setData(Uri.parse("http://maps.google.com/maps?saddr=" + src_lat + "," + src_ltg + "&daddr=" + des_lat + "," + des_ltg)); startActivity(intent); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); } /** * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly * installed) and the map has not already been instantiated.. This will ensure that we only ever * call {@link #setUpMap()} once when {@link #gMap} is not null. * <p/> * If it isn't installed {@link SupportMapFragment} (and * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to * install/update the Google Play services APK on their device. * <p/> * A user can return to this FragmentActivity after following the prompt and correctly * installing/updating/enabling the Google Play services. Since the FragmentActivity may not * have been completely destroyed during this process (it is likely that it would only be * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this * method in {@link #onResume()} to guarantee that it will be called. */ private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (gMap == null) { // Try to obtain the map from the SupportMapFragment. gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); // Check if we were successful in obtaining the map. if (gMap != null) { setUpMap(); } } } /** * This is where we can add markers or lines, add listeners or move the camera. In this case, we * just add a marker near Africa. * <p/> * This should only be called once and when we are sure that {@link #gMap} is not null. */ private void setUpMap() { gMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); } }