Last active
December 20, 2018 02:48
-
-
Save Felipe00/0441ed8a4c70c6f680515660a4e5d270 to your computer and use it in GitHub Desktop.
Pegar lista de pontos entre 2 coordenadas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.ContextWrapper; | |
import android.content.Intent; | |
import android.media.RingtoneManager; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.net.Uri; | |
import android.support.v4.app.NotificationCompat; | |
import android.util.Log; | |
import com.google.android.gms.maps.model.LatLng; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.gson.Gson; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
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 br.com.interaje.soli.driver.R; | |
import br.com.interaje.soli.driver.features.openCall.OpenCallActivity; | |
import br.com.interaje.soli.driver.models.Geometry; | |
import br.com.interaje.soli.driver.models.Passenger; | |
import br.com.interaje.soli.driver.models.Trip; | |
/** | |
* Created by javab0y on 26/06/17. | |
*/ | |
public class AppHelper extends ContextWrapper { | |
public AppHelper(Context base) { | |
super(base); | |
} | |
/** | |
* Verifica se a rede está disponível | |
* | |
* @return | |
*/ | |
public boolean isNetworkAvailable() { | |
ConnectivityManager connectivityManager = (ConnectivityManager) getBaseContext().getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); | |
return activeNetworkInfo != null && activeNetworkInfo.isConnected(); | |
} | |
public String getDirectionsUrl(LatLng origin, LatLng dest) { | |
// Origin of route | |
String str_origin = "origin=" + origin.latitude + "," + origin.longitude; | |
// Destination of route | |
String str_dest = "destination=" + dest.latitude + "," + dest.longitude; | |
// Sensor enabled | |
String sensor = "sensor=false"; | |
String mode = "mode=driving"; | |
// Building the parameters to the web service | |
String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + mode; | |
// Output format | |
String output = "json"; | |
// Building the url to the web service | |
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; | |
return url; | |
} | |
public 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; | |
} | |
public static void buildNotificaitonOpenCall(Context ctx, Trip trip) { | |
Intent openCallIntent = new Intent(ctx, OpenCallActivity.class); | |
openCallIntent.putExtra("trip", trip); | |
openCallIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, openCallIntent, PendingIntent.FLAG_ONE_SHOT); | |
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx) | |
.setSmallIcon(R.mipmap.ic_launcher_round) | |
.setContentTitle("SOLI") | |
.setContentText("Você tem uma nova viagem!") | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = | |
(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify((int) System.currentTimeMillis(), notificationBuilder.build()); | |
} | |
public static Trip snapshotToTrip(DataSnapshot dataSnapshot) throws JSONException { | |
Trip trip = new Trip(); | |
double lat, lng; | |
JSONArray jsonArray; | |
for (DataSnapshot data : dataSnapshot.getChildren()) { | |
if (data.getKey().equals("from")) { | |
jsonArray = new JSONArray(data.getValue().toString()); | |
lat = jsonArray.getDouble(0); | |
lng = jsonArray.getDouble(1); | |
trip.setCurrentLocation(new Geometry(lat, lng)); | |
} | |
if (data.getKey().equals("to")) { | |
jsonArray = new JSONArray(data.getValue().toString()); | |
lat = jsonArray.getDouble(0); | |
lng = jsonArray.getDouble(1); | |
trip.setDestineLocation(new Geometry(lat, lng)); | |
} | |
if (data.getKey().equals("passenger")) { | |
JSONObject obj = new JSONObject(new Gson().toJson(data.getValue())); | |
trip.setPassenger(new Passenger(obj)); | |
} | |
if (data.getKey().equals("status")) { | |
trip.setStatus(Integer.parseInt(data.getValue().toString())); | |
} | |
} | |
return trip; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void buildRoute(LatLng origin, LatLng dest) { | |
// Getting URL to the Google Directions API | |
String url = (new AppHelper(this)).getDirectionsUrl(origin, dest); | |
DownloadTask downloadTask = new DownloadTask(); | |
// Start downloading json data from Google Directions API | |
downloadTask.execute(url); | |
} | |
private LatLng getPolygonCenterPoint(List<LatLng> polygonPointsList) { | |
LatLng centerLatLng = null; | |
LatLngBounds.Builder builder = new LatLngBounds.Builder(); | |
for (int i = 0; i < polygonPointsList.size(); i++) { | |
builder.include(polygonPointsList.get(i)); | |
} | |
LatLngBounds bounds = builder.build(); | |
centerLatLng = bounds.getCenter(); | |
return centerLatLng; | |
} | |
private Bitmap getBitmap(int drawable) { | |
Bitmap icon = BitmapFactory.decodeResource(getResources(), drawable); | |
Bitmap resized = icon.createScaledBitmap(icon, 100, 100, false); | |
return resized; | |
} | |
private class DownloadTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... url) { | |
String data = ""; | |
try { | |
data = (new AppHelper(OpenCallActivity.this)).downloadUrl(url[0]); | |
} catch (Exception e) { | |
Log.d("Background Task", e.toString()); | |
} | |
return data; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
super.onPostExecute(result); | |
ParserTask parserTask = new ParserTask(); | |
parserTask.execute(result); | |
} | |
} | |
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> { | |
// Parsing the data in non-ui thread | |
@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]); | |
DirectionsJSONParser parser = new DirectionsJSONParser(); | |
routes = parser.parse(jObject); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return routes; | |
} | |
@Override | |
protected void onPostExecute(List<List<HashMap<String, String>>> result) { | |
ArrayList points = null; | |
PolylineOptions lineOptions = null; | |
MarkerOptions markerOptions = new MarkerOptions(); | |
LatLngBounds.Builder builder = new LatLngBounds.Builder(); | |
for (int i = 0; i < result.size(); i++) { | |
points = new ArrayList(); | |
lineOptions = new PolylineOptions(); | |
List<HashMap<String, String>> path = result.get(i); | |
for (int j = 0; j < path.size(); j++) { | |
HashMap point = path.get(j); | |
double lat = Double.parseDouble(point.get("lat").toString()); | |
double lng = Double.parseDouble(point.get("lng").toString()); | |
LatLng position = new LatLng(lat, lng); | |
points.add(position); | |
builder.include(position); | |
} | |
lineOptions.addAll(points); | |
lineOptions.width(6); | |
lineOptions.color(Color.RED); | |
lineOptions.geodesic(true); | |
} | |
if (polyline == null && lineOptions != null) { | |
polyline = mMap.addPolyline(lineOptions); | |
} else { | |
if (points != null) { | |
polyline.setPoints(points); | |
} | |
} | |
if (result.size() > 0) { | |
/**initialize the padding for map boundary*/ | |
int padding = 50; | |
/**create the bounds from latlngBuilder to set into map camera*/ | |
LatLngBounds bounds = builder.build(); | |
/**create the camera with bounds and padding to set into map*/ | |
final CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding); | |
/**call the map call back to know map is loaded or not*/ | |
// Drawing polyline in the Google Map for the i-th route | |
mMap.animateCamera(cameraUpdate); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.android.gms.maps.model.LatLng; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* Created by anupamchugh on 27/11/15. | |
*/ | |
public class DirectionsJSONParser { | |
/** | |
* Receives a JSONObject and returns a list of lists containing latitude and longitude | |
*/ | |
public List<List<HashMap<String, String>>> parse(JSONObject jObject) { | |
List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>(); | |
JSONArray jRoutes = null; | |
JSONArray jLegs = null; | |
JSONArray jSteps = null; | |
try { | |
jRoutes = jObject.getJSONArray("routes"); | |
/** Traversing all routes */ | |
for (int i = 0; i < jRoutes.length(); i++) { | |
jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs"); | |
List path = new ArrayList<HashMap<String, String>>(); | |
/** Traversing all legs */ | |
for (int j = 0; j < jLegs.length(); j++) { | |
jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps"); | |
/** Traversing all steps */ | |
for (int k = 0; k < jSteps.length(); k++) { | |
String polyline = ""; | |
polyline = (String) ((JSONObject) ((JSONObject) jSteps.get(k)).get("polyline")).get("points"); | |
List list = decodePoly(polyline); | |
/** Traversing all points */ | |
for (int l = 0; l < list.size(); l++) { | |
HashMap<String, String> hm = new HashMap<String, String>(); | |
hm.put("lat", Double.toString(((LatLng) list.get(l)).latitude)); | |
hm.put("lng", Double.toString(((LatLng) list.get(l)).longitude)); | |
path.add(hm); | |
} | |
} | |
routes.add(path); | |
} | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
} | |
return routes; | |
} | |
/** | |
* Method to decode polyline points | |
* Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java | |
*/ | |
private List decodePoly(String encoded) { | |
List poly = new ArrayList(); | |
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))); | |
poly.add(p); | |
} | |
return poly; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment