Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // rota inicial,e rota da unit
- public void getEndereco() throws UnsupportedEncodingException {
- String o = String.valueOf(latitude +","+ longitude);
- String d = "Universidade Tiradentes - Avenida Murilo Dantas - Farolândia, Aracaju - SE";
- String origin = URLEncoder.encode(o, "UTF-8");
- String destination = URLEncoder.encode(d, "UTF-8");
- }
- // WEB CONNECTION
- public void getRoute(final String origin, final String destination){
- new Thread(){
- public void run(){
- String url= "http://maps.googleapis.com/maps/api/directions/json?origin="
- + origin+"&destination="
- + destination+"&sensor=false";
- HttpResponse response;
- HttpGet request;
- AndroidHttpClient client = AndroidHttpClient.newInstance("route");
- request = new HttpGet(url);
- try {
- response = client.execute(request);
- final String answer = EntityUtils.toString(response.getEntity());
- runOnUiThread(new Runnable(){
- public void run(){
- try {
- //Log.i("Script", answer);
- list = buildJSONRoute(answer);
- drawRoute();
- }
- catch(JSONException e) {
- e.printStackTrace();
- }
- }
- });
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- //JSON
- public List<LatLng> buildJSONRoute(String json) throws JSONException {
- JSONObject result = new JSONObject(json);
- JSONArray routes = result.getJSONArray("routes");
- long distanceForSegment = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getInt("values");
- JSONArray steps = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");
- List<LatLng> lines = new ArrayList<LatLng>();
- for (int i = 0; i < steps.length(); i++) {
- Log.i("Script", "STEP: LAT: " + steps.getJSONObject(i).getJSONObject("start_location").getDouble("lat") + " | LNG: " + steps.getJSONObject(i).getJSONObject("start_location").getDouble("lng"));
- String polyline = steps.getJSONObject(i).getJSONObject("polyline").getString("points");
- for (LatLng p : decodePolyline(polyline)) {
- lines.add(p);
- }
- Log.i("Script", "STEP: LAT: " + steps.getJSONObject(i).getJSONObject("end_location").getDouble("lat") + " | LNG: " + steps.getJSONObject(i).getJSONObject("end_location").getDouble("lng"));
- }
- return lines;
- }
- // DECODE POLYLINE
- private List<LatLng> decodePolyline(String encoded) {
- List<LatLng> listPoints = new ArrayList<LatLng>();
- int index = 0, len = encoded.length();
- int lat = 0, lng = 0;
- while (index < len) {
- int b, shift = 0, result = 0;
- do {
- b = encoded.charAt(index++) - 63;
- result |= (b & 0x1f) << shift;
- shift += 5;
- } while (b >= 0x20);
- int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
- lat += dlat;
- shift = 0;
- result = 0;
- do {
- b = encoded.charAt(index++) - 63;
- result |= (b & 0x1f) << shift;
- shift += 5;
- } while (b >= 0x20);
- int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
- lng += dlng;
- LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5)));
- Log.i("Script", "POL: LAT: " + p.latitude + " | LNG: " + p.longitude);
- listPoints.add(p);
- }
- return listPoints;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement