Last active
April 24, 2017 07:09
-
-
Save alfianyusufabdullah/15cc5fb4367feb625ba05a0dd24f4af0 to your computer and use it in GitHub Desktop.
Google Direction Request
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
package aku.sayang.mantan; | |
import com.google.android.gms.maps.model.LatLng; | |
import java.util.ArrayList; | |
/** | |
* Created by Masx Developer on 4/24/17. | |
* https://masx-dev.blogspot.com | |
*/ | |
public class ModelRute { | |
private LatLng Northeast; | |
private LatLng Soutwest; | |
private String Distanse; | |
private String Duration; | |
private ArrayList<LatLng> Direction; | |
public LatLng getNortheast() { | |
return Northeast; | |
} | |
public void setNortheast(LatLng northeast) { | |
Northeast = northeast; | |
} | |
public LatLng getSoutwest() { | |
return Soutwest; | |
} | |
public void setSoutwest(LatLng soutwest) { | |
Soutwest = soutwest; | |
} | |
public String getDistanse() { | |
return Distanse; | |
} | |
public void setDistanse(String distanse) { | |
Distanse = distanse; | |
} | |
public String getDuration() { | |
return Duration; | |
} | |
public void setDuration(String duration) { | |
Duration = duration; | |
} | |
public ArrayList<LatLng> getDirection() { | |
return Direction; | |
} | |
public void setDirection(ArrayList<LatLng> direction) { | |
Direction = direction; | |
} | |
} |
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
package aku.sayang.mantan; | |
import android.util.Log; | |
import com.google.android.gms.maps.model.LatLng; | |
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.util.ArrayList; | |
import java.util.List; | |
import aku.sayang.mantan.ModelRute; | |
import okhttp3.Call; | |
import okhttp3.Callback; | |
import okhttp3.HttpUrl; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
/** | |
* Created by Masx Developer on 4/24/17. | |
* https://masx-dev.blogspot.com | |
*/ | |
public class Rute { | |
private OnRuteListener listener; | |
private static final String TAG = Rute.class.getSimpleName(); | |
public Rute(OnRuteListener listener) { | |
this.listener = listener; | |
} | |
public void getRute(LatLng from, LatLng to) { | |
HttpUrl.Builder urlBuider = HttpUrl.parse("http://maps.googleapis.com/maps/api/directions/json?").newBuilder(); | |
urlBuider.addQueryParameter("origin", from.latitude + "," + from.longitude); | |
urlBuider.addQueryParameter("destination", to.latitude + "," + to.longitude); | |
urlBuider.addQueryParameter("sensor", "false"); | |
urlBuider.addQueryParameter("mode", "driving"); | |
OkHttpClient client = new OkHttpClient(); | |
Request request = new Request.Builder() | |
.url(urlBuider.build().toString()) | |
.build(); | |
client.newCall(request).enqueue(new Callback() { | |
@Override | |
public void onFailure(Call call, IOException e) { | |
listener.onError("Terjadi Kesalahan Jaringan"); | |
Log.d(TAG, "onFailure: " + e.getLocalizedMessage()); | |
} | |
@Override | |
public void onResponse(Call call, Response response) throws IOException { | |
try { | |
InputStream Is = response.body().byteStream(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(Is)); | |
StringBuilder stringBuilder = new StringBuilder(); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
stringBuilder.append(line).append("\n"); | |
} | |
String DATA = stringBuilder.toString(); | |
LatLng South = getSouthwest(DATA); | |
LatLng North = getNortheast(DATA); | |
String Distances = getDistance(DATA); | |
String Duration = getDuration(DATA); | |
ArrayList<LatLng> Direction = getDirection(DATA); | |
ModelRute DataRute = new ModelRute(); | |
DataRute.setSoutwest(South); | |
DataRute.setNortheast(North); | |
DataRute.setDistanse(Distances); | |
DataRute.setDuration(Duration); | |
DataRute.setDirection(Direction); | |
listener.onSuccess(DataRute); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
listener.onError("Terjadi Kesalahan Saat Memuat Data"); | |
Log.d(TAG, "onResponse: " + e.getLocalizedMessage()); | |
} | |
} | |
}); | |
} | |
private LatLng getNortheast(String Data) { | |
try { | |
JSONObject DataJson = new JSONObject(Data); | |
JSONArray Routes = DataJson.getJSONArray("routes"); | |
JSONObject RoutesObj = Routes.optJSONObject(0); | |
JSONObject Bounds = RoutesObj.getJSONObject("bounds"); | |
JSONObject NorthData = Bounds.getJSONObject("northeast"); | |
double Latitude = NorthData.getDouble("lat"); | |
double Longitude = NorthData.getDouble("lng"); | |
return new LatLng(Latitude, Longitude); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private LatLng getSouthwest(String Data) { | |
try { | |
JSONObject DataJson = new JSONObject(Data); | |
JSONArray Routes = DataJson.getJSONArray("routes"); | |
JSONObject RoutesObj = Routes.optJSONObject(0); | |
JSONObject Bounds = RoutesObj.getJSONObject("bounds"); | |
JSONObject NorthData = Bounds.getJSONObject("southwest"); | |
double Latitude = NorthData.getDouble("lat"); | |
double Longitude = NorthData.getDouble("lng"); | |
return new LatLng(Latitude, Longitude); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private String getDistance(String Data) { | |
try { | |
JSONObject DataJson = new JSONObject(Data); | |
JSONArray Routes = DataJson.getJSONArray("routes"); | |
JSONObject RoutesObj = Routes.optJSONObject(0); | |
JSONArray LegsObj = RoutesObj.getJSONArray("legs"); | |
JSONObject Legs = LegsObj.getJSONObject(0); | |
JSONObject Distance = Legs.getJSONObject("distance"); | |
return Distance.getString("text"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private String getDuration(String Data) { | |
try { | |
JSONObject DataJson = new JSONObject(Data); | |
JSONArray Routes = DataJson.getJSONArray("routes"); | |
JSONObject RoutesObj = Routes.optJSONObject(0); | |
JSONArray LegsObj = RoutesObj.getJSONArray("legs"); | |
JSONObject Legs = LegsObj.getJSONObject(0); | |
JSONObject Duration = Legs.getJSONObject("duration"); | |
return Duration.getString("text"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private ArrayList<LatLng> getDirection(String Data){ | |
ArrayList<LatLng> direction = new ArrayList<>(); | |
try { | |
JSONObject DataJson = new JSONObject(Data); | |
JSONArray Routes = DataJson.getJSONArray("routes"); | |
JSONObject RoutesObj = Routes.optJSONObject(0); | |
JSONArray LegsObj = RoutesObj.getJSONArray("legs"); | |
JSONObject Legs = LegsObj.getJSONObject(0); | |
JSONArray Steps = Legs.getJSONArray("steps"); | |
for (int i = 0; i < Steps.length(); i++) { | |
JSONObject data = Steps.getJSONObject(i); | |
JSONObject start = data.getJSONObject("start_location"); | |
JSONObject Polyline = data.getJSONObject("polyline"); | |
JSONObject end = data.getJSONObject("end_location"); | |
double startLat = start.getDouble("lat"); | |
double startLng = start.getDouble("lng"); | |
direction.add(new LatLng(startLat,startLng)); | |
String decode = Polyline.getString("points"); | |
List<LatLng> decodePolyline = decodePolyline(decode); | |
direction.addAll(decodePolyline); | |
double endLat = end.getDouble("lat"); | |
double endLng = end.getDouble("lng"); | |
direction.add(new LatLng(endLat, endLng)); | |
} | |
return direction; | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static ArrayList<LatLng> decodePolyline(String polyline) { | |
ArrayList<LatLng> decodePoly = new ArrayList<>(); | |
int index = 0, len = polyline.length(); | |
int lat = 0, lng = 0; | |
while (index < len) { | |
int b, shift = 0, result = 0; | |
do { | |
b = polyline.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 = polyline.charAt(index++) - 63; | |
result |= (b & 0x1f) << shift; | |
shift += 5; | |
} while (b >= 0x20); | |
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); | |
lng += dlng; | |
LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5); | |
decodePoly.add(position); | |
} | |
return decodePoly; | |
} | |
public interface OnRuteListener { | |
void onSuccess(ModelRute Data); | |
void onError(String Error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment