Created
September 21, 2017 13:37
-
-
Save johnteee/dab6d3ca4572efd95cf8e30f7cbc5aa1 to your computer and use it in GitHub Desktop.
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
public Document getDocument(LatLng start, LatLng end, List<LatLng> wayPoints, TravelMode mode) { | |
String url = "https://maps.googleapis.com/maps/api/directions/xml?" | |
+ "origin=" + start.latitude + "," + start.longitude | |
+ "&destination=" + end.latitude + "," + end.longitude | |
+ "&sensor=false&units=metric&mode=" + mode.toString().toLowerCase() | |
+ "&key=" + MyApplication.getInstance().getString(R.string.API_KEY_GOOGLE_GEO) | |
; | |
if (wayPoints != null && wayPoints.size() > 0) { | |
url += "&waypoints="; | |
int size = wayPoints.size(); | |
for (int i = 0; i < size; i ++) { | |
LatLng latLng = wayPoints.get(i); | |
url += String.format("via:%.5f%%2C%.5f", latLng.latitude, latLng.longitude); | |
if (i < size - 1) { | |
url += "%7C"; | |
} | |
} | |
} | |
Log.d("url", url); | |
try { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpContext localContext = new BasicHttpContext(); | |
HttpPost httpPost = new HttpPost(url); | |
HttpResponse response = httpClient.execute(httpPost, localContext); | |
InputStream in = response.getEntity().getContent(); | |
DocumentBuilder builder = DocumentBuilderFactory.newInstance() | |
.newDocumentBuilder(); | |
Document doc = builder.parse(in); | |
return doc; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment