Skip to content

Instantly share code, notes, and snippets.

@William-ST
Created March 29, 2016 21:06
Show Gist options
  • Save William-ST/ebb3533b32a36fd639413858a4bb4e8e to your computer and use it in GitHub Desktop.
Save William-ST/ebb3533b32a36fd639413858a4bb4e8e to your computer and use it in GitHub Desktop.
Clase para dibujar rutas en google maps
import android.graphics.Color;
import android.graphics.Point;
import android.location.Address;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
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.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.model.VisibleRegion;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import me.doapps.etaxiclient.R;
/**
* Created by William_ST on 10/02/16.
*/
public class MapRouteUtil {
private GoogleMap mMap;
private Polyline line;
private InterfaceEndDraw interfaceEndDraw;
public final String TAG = MapRouteUtil.class.getSimpleName();
public MapRouteUtil(GoogleMap mMap){
this.mMap = mMap;
}
public void traceRourte(LatLng origin, LatLng destine){
if(origin != null && destine != null) {
new GetDirectionsAsync().execute(origin, destine);
}
}
class GetDirectionsAsync extends AsyncTask<LatLng, Void, List<LatLng>> {
JSONParser jsonParser;
String DIRECTIONS_URL = "https://maps.googleapis.com/maps/api/directions/json?sensor=false&language=pt";
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<LatLng> doInBackground(LatLng... params) {
LatLng start = params[0];
LatLng end = params[1];
HashMap<String, String> points = new HashMap<>();
points.put("origin", start.latitude + "," + start.longitude);
points.put("destination", end.latitude + "," + end.longitude);
points.put("mode","driving");
jsonParser = new JSONParser();
JSONObject obj = jsonParser.makeHttpRequest(DIRECTIONS_URL, "GET", points, true);
if (obj == null) return null;
try {
List<LatLng> list = null;
JSONArray routeArray = obj.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
list = decodePolylines(encodedString);
return list;
} catch (JSONException e) {
Log.e(TAG, "doInbackground: "+e.toString());
}
return null;
}
@Override
protected void onPostExecute(List<LatLng> pointsList) {
try {
if (pointsList == null) return;
if (line != null) {
line.remove();
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
PolylineOptions options = new PolylineOptions().width(5).color(R.color.route_line).geodesic(true);
for (int i = 0; i < pointsList.size(); i++) {
LatLng point = pointsList.get(i);
builder.include(pointsList.get(i));
options.add(point);
}
LatLngBounds bounds = builder.build();
if (options != null) {
line = mMap.addPolyline(options);
//verificar si el mapa se asigna cuando ya esta instanciado
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
mMap.animateCamera(cu, 200, null);
// LatLng auxLocation = getCenterPosition();
// mMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
// .target(new LatLng(auxLocation.latitude, auxLocation.longitude))
// .zoom(16)
// .build()));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
interfaceEndDraw.ready(line);
}
}, 1000);
}
} catch (NullPointerException e){
Log.e(TAG, "onPostExecute "+e.toString());
}
}
}
private List<LatLng> decodePolylines(final String encodedPoints) {
List<LatLng> lstLatLng = new ArrayList<LatLng>();
int index = 0;
int lat = 0, lng = 0;
while (index < encodedPoints.length()) {
int b, shift = 0, result = 0;
do {
b = encodedPoints.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 = encodedPoints.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
lstLatLng.add(new LatLng((double) lat / 1E5, (double) lng / 1E5));
}
return lstLatLng;
}
public interface InterfaceEndDraw{
void ready(Polyline polyline);
}
public void setInterfaceEndDraw(InterfaceEndDraw interfaceEndDraw){
this.interfaceEndDraw = interfaceEndDraw;
}
public LatLng getCenterPosition() {
VisibleRegion visibleRegion = mMap.getProjection()
.getVisibleRegion();
Point x = mMap.getProjection().toScreenLocation(
visibleRegion.farRight);
Point y = mMap.getProjection().toScreenLocation(
visibleRegion.nearLeft);
Point centerPoint = new Point(x.x / 2, y.y / 2);
return mMap.getProjection().fromScreenLocation(centerPoint);
}
}
@William-ST
Copy link
Author

package me.doapps.etaxidriver.util;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

/**

  • Created by William_ST on 13/04/16.
    */
    public class JSONParser {
    private String charset = "UTF-8";
    private HttpURLConnection conn;
    private DataOutputStream wr;
    private StringBuilder result;
    private URL urlObj;
    private JSONObject jObj = null;
    private StringBuilder sbParams;
    private String paramsString;
    private final String TAG = JSONParser.class.getSimpleName();

    public JSONObject makeHttpRequest(String url, String method, HashMap<String, String> params, boolean encode) {

    sbParams = new StringBuilder();
    int i = 0;
    for (String key : params.keySet()) {
        try {
            if (i != 0){
                sbParams.append("&");
            }
            if (encode) {
                sbParams.append(key).append("=")
                        .append(URLEncoder.encode(params.get(key), charset));
            }
            else{
                sbParams.append(key).append("=")
                        .append(params.get(key));
            }
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        i++;
    }
    
    if (method.equals("POST")) {
        // request method is POST
        try {
            urlObj = new URL(url);
    
            conn = (HttpURLConnection) urlObj.openConnection();
    
            conn.setDoOutput(true);
    
            conn.setRequestMethod("POST");
    
            conn.setRequestProperty("Accept-Charset", charset);
    
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
    
            conn.connect();
    
            paramsString = sbParams.toString();
    
            wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(paramsString);
            wr.flush();
            wr.close();
    
        } catch (IOException e) {
            Log.e(TAG, "if method POST: " + e.toString());
        }
    }
    else if(method.equals("GET")){
        if (sbParams.length() != 0) {
            url +="&"+ sbParams.toString();
        }
        try {
            urlObj = new URL(url);
            conn = (HttpURLConnection) urlObj.openConnection();
            conn.setDoOutput(false);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept-Charset", charset);
            conn.setConnectTimeout(15000);
            conn.connect();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }
    }
    
    try {
        //Receive the response from the server
        InputStream in = new BufferedInputStream(conn.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    
        String line;
        result = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }
    } catch (IOException e) {
        Log.e(TAG, "try "+e.toString());
    }
    
    conn.disconnect();
    
    try {
        jObj = new JSONObject(result.toString());
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } catch (NullPointerException e){
        Log.e(TAG, "e-makeHttp "+e.toString());
    }
    
    return jObj;
    

    }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment