Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5.  
  6. import org.apache.http.client.ClientProtocolException;
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import android.util.Log;
  12.  
  13. import co.easycabs.cabbookinghome.R;
  14. import com.google.android.gms.maps.CameraUpdateFactory;
  15. import com.google.android.gms.maps.GoogleMap;
  16. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  17. import com.google.android.gms.maps.model.CameraPosition;
  18. import com.google.android.gms.maps.model.LatLng;
  19. import com.google.android.gms.maps.model.Marker;
  20. import com.google.android.gms.maps.model.MarkerOptions;
  21.  
  22. import epbit.Login.LoginDetails;
  23. import epbit.exception.NetworkException;
  24. import epbit.exception.ServerException;
  25. import epbit.service.HttpService;
  26.  
  27. public class MapUtil {
  28.  
  29. /**
  30. *
  31. * Method to Drop Map Pointer to Current Location
  32. *
  33. * @param google_map
  34. * Google Map object
  35. * @param lat
  36. * latitude of current location
  37. * @param longitude
  38. * longitude of current location icon_color of Icon to be Pointed
  39. * will be Blue
  40. */
  41.  
  42. public static void dropPin(GoogleMap google_map, double lat, double longi) {
  43. MarkerOptions options = new MarkerOptions();
  44. options.position(new LatLng(lat, longi));
  45. options.icon(BitmapDescriptorFactory
  46. .fromResource(R.drawable.map_pointer));
  47. options.icon(BitmapDescriptorFactory
  48. .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
  49.  
  50. google_map.addMarker(options);
  51.  
  52. }
  53.  
  54. /**
  55. *
  56. * Method to Drop Map Pointer to Current Location
  57. *
  58. * @param google_map
  59. * Google Map object
  60. * @param lat
  61. * latitude of current location
  62. * @param longitude
  63. * longitude of current location
  64. * @param icon_color
  65. * color of Icon to be Pointed
  66. * @return
  67. */
  68.  
  69. public static Marker dropPin(GoogleMap google_map, double lat,
  70. double longi, int icon, String title) {
  71. MarkerOptions options = new MarkerOptions();
  72. options.position(new LatLng(lat, longi));
  73. options.icon(BitmapDescriptorFactory.fromResource(icon));
  74. if (title != null)
  75. options.title(title);
  76.  
  77. return google_map.addMarker(options);
  78.  
  79. }
  80.  
  81. /**
  82. * Method to Clear the All the Markers Pointers from the map
  83. *
  84. * @param google_map
  85. */
  86. public static void clearMarkers(GoogleMap google_map) {
  87. google_map.clear();
  88. }
  89.  
  90. /**
  91. * Method to Change Camera Focus to Current Location
  92. *
  93. * @param google_map
  94. * Google Map object
  95. * @param lat
  96. * latitude of current location
  97. * @param longitude
  98. * longitude of current location
  99. * @param zoomlevel
  100. * ZoomLevel to which focus had be done
  101. */
  102. public static void changeCameraFocus(GoogleMap google_map, double lat,
  103. double longitude, int zoomlevel) {
  104.  
  105. CameraPosition cameraPosition = new CameraPosition.Builder()
  106. .target(new LatLng(lat, longitude)).zoom(zoomlevel).build();
  107. google_map.animateCamera(CameraUpdateFactory
  108. .newCameraPosition(cameraPosition));
  109. }
  110.  
  111. public static String getLatLongToAddressResponse(double lat, double longi,
  112. String google_api_key) throws ClientProtocolException, IOException,
  113. NetworkException, ServerException, JSONException {
  114.  
  115. String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="
  116. + lat + "," + longi + "&sensor=true";
  117. Log.e("url ", url);
  118. String res = HttpService.httpPostService(url);
  119. Log.e("response ", res);
  120. JSONObject json = new JSONObject(res);
  121. if (json.has("results")) {
  122.  
  123. JSONArray jArray = json.getJSONArray("results");
  124. JSONObject json1 = jArray.getJSONObject(0);
  125. if (json1.has("formatted_address")) {
  126. res = json1.getString("formatted_address");
  127. } else {
  128. throw new JSONException(json.toString());
  129. }
  130. }
  131.  
  132. return res;
  133. }
  134.  
  135. public static List<String> getAddressSuggestions(String api_key,
  136. String keyword, String lat, String longi)
  137. throws ClientProtocolException, IOException, NetworkException,
  138. ServerException, JSONException {
  139. String search_text[] = keyword.replace(" ", "").split(",");
  140.  
  141. String url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input="
  142. + search_text[0]
  143. + "&location="
  144. + lat
  145. + ","
  146. + longi
  147. + "&radius=49000&sensor=true&key=" + api_key;
  148. Log.i("auto complete uri ", url);
  149.  
  150. List<String> list = new ArrayList<String>();
  151. String res = HttpService.httpPostService(url);
  152. JSONObject json = new JSONObject(res);
  153. if (json != null && json.has("predictions")) {
  154.  
  155. JSONArray contacts = json.getJSONArray("predictions");
  156.  
  157. for (int i = 0; i < contacts.length(); i++) {
  158. JSONObject c = contacts.getJSONObject(i);
  159. if (c.has("description")) {
  160. String description = c.getString("description");
  161.  
  162. list.add(description);
  163. }
  164.  
  165. }
  166. }
  167. return list;
  168.  
  169. }
  170. public static String getLatLongFromAddress(String address)
  171. throws ClientProtocolException, IOException, NetworkException,
  172. ServerException, JSONException {
  173.  
  174. String uri = "http://maps.google.com/maps/api/geocode/json?address="
  175. + address.replace(" ", "") + "&sensor=false";
  176.  
  177. String res = HttpService.httpPostService(uri);
  178. String location = "";
  179.  
  180. JSONObject jsonObject = new JSONObject(res);
  181. if (jsonObject != null && jsonObject.has("results")) {
  182. JSONArray jarray = jsonObject.getJSONArray("results");
  183. if (jarray != null && jarray.length() > 0) {
  184. JSONObject json = jarray.getJSONObject(0);
  185. if (json != null && json.has("geometry")) {
  186. JSONObject loca = json.getJSONObject("geometry");
  187. if(loca!=null&&loca.has("location"))
  188. {
  189. JSONObject loc=loca.getJSONObject("location");
  190. if (loc.has("lat")) {
  191. location+=loc.getDouble("lat");
  192. }
  193. location+=",";
  194. if (loc.has("lng")) {
  195. location+=loc.getDouble("lng");
  196. }
  197. }
  198. }
  199.  
  200.  
  201. }
  202.  
  203. }
  204.  
  205. return location;
  206. }
  207.  
  208.  
  209. public static HashMap<String, String> getTimeDistanceBetweenMapPoints(
  210. String source_lat, String source_long, String dest_lat,
  211. String dest_long, String units) throws ClientProtocolException,
  212. IOException, NetworkException, ServerException, JSONException {
  213. HashMap<String, String> response = new HashMap<String, String>();
  214.  
  215. String url = "https://maps.googleapis.com/maps/api/directions/"
  216. + "json" + "?" + "&origin=" + source_lat
  217. + "," + source_long + "&destination=" + dest_lat + ","
  218. + dest_long + "&" + "sensor=false" + "&" + "units=" + units;
  219. //
  220. // String url="http://maps.googleapis.com/maps/api/directions/json?origin=28.675861870844997,77.32144810259342&destination=28.6805906,77.3747908&sensor=true&mode=driving";
  221. String res = "";
  222. res = HttpService.httpPostService(url);
  223. response.put("response", res);
  224. JSONObject jsonobj = new JSONObject(res);
  225. if (jsonobj != null && jsonobj.has("routes")) {
  226. JSONArray jsonarray = jsonobj.getJSONArray("routes");
  227.  
  228. if (jsonarray != null && jsonarray.length() > 0) {
  229. JSONObject jsonobj2 = jsonarray.getJSONObject(0);
  230. if (jsonobj2 != null && jsonobj2.has("legs")) {
  231. JSONArray jsonArray2 = jsonobj2.getJSONArray("legs");
  232. if (jsonArray2 != null && jsonArray2.length() > 0) {
  233.  
  234. JSONObject jsonobj3 = jsonArray2.getJSONObject(0);
  235. if (jsonobj3 != null && jsonobj3.has("distance")) {
  236. JSONObject jsonobj4 = jsonobj3
  237. .getJSONObject("distance");
  238. if (jsonobj4 != null && jsonobj4.has("text")) {
  239. LoginDetails.S_D_Distance = jsonobj4
  240. .getString("text");
  241. response.put("distance",
  242. LoginDetails.S_D_Distance);
  243. }
  244. }
  245. if (jsonobj3 != null && jsonobj3.has("duration")) {
  246. JSONObject jsonobj4 = jsonobj3
  247. .getJSONObject("duration");
  248. if (jsonobj4 != null && jsonobj4.has("text")) {
  249. LoginDetails.S_D_Time = jsonobj4
  250. .getString("text");
  251. response.put("duration", LoginDetails.S_D_Time);
  252. }
  253. }
  254.  
  255. }
  256.  
  257. }
  258.  
  259. }
  260.  
  261. }
  262.  
  263. return response;
  264. }
  265.  
  266. public static List<List<HashMap<String, String>>> getRoutesBetweenMapPoints(
  267. String res) throws JSONException {
  268. List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String, String>>>();
  269.  
  270. JSONArray jLegs;
  271. JSONObject jsonobj = new JSONObject(res);
  272. if (jsonobj != null && jsonobj.has("routes")) {
  273. JSONArray jsonarray = jsonobj.getJSONArray("routes");
  274.  
  275. if (jsonarray != null && jsonarray.length() > 0) {
  276.  
  277. for (int i = 0; i < jsonarray.length(); i++) {
  278. jLegs = ((JSONObject) jsonarray.get(i))
  279. .getJSONArray("legs");
  280. List<HashMap<String, String>> path = new ArrayList<HashMap<String, String>>();
  281.  
  282. /** Traversing all legs */
  283. for (int j = 0; j < jLegs.length(); j++) {
  284. JSONArray jSteps = ((JSONObject) jLegs.get(j))
  285. .getJSONArray("steps");
  286.  
  287. /** Traversing all steps */
  288. for (int k = 0; k < jSteps.length(); k++) {
  289. String polyline = "";
  290. polyline = (String) ((JSONObject) ((JSONObject) jSteps
  291. .get(k)).get("polyline")).get("points");
  292. List<LatLng> list = decodePoly(polyline);
  293.  
  294. /** Traversing all points */
  295. for (int l = 0; l < list.size(); l++) {
  296. HashMap<String, String> hm = new HashMap<String, String>();
  297. hm.put("lat", Double.toString(((LatLng) list
  298. .get(l)).latitude));
  299. hm.put("lng", Double.toString(((LatLng) list
  300. .get(l)).longitude));
  301. path.add(hm);
  302. }
  303. }
  304. routes.add(path);
  305. }
  306.  
  307. }
  308. }
  309. }
  310. return routes;
  311. }
  312.  
  313. private static List<LatLng> decodePoly(String encoded) {
  314.  
  315. List<LatLng> poly = new ArrayList<LatLng>();
  316. int index = 0, len = encoded.length();
  317. int lat = 0, lng = 0;
  318.  
  319. while (index < len) {
  320. int b, shift = 0, result = 0;
  321. do {
  322. b = encoded.charAt(index++) - 63;
  323. result |= (b & 0x1f) << shift;
  324. shift += 5;
  325. } while (b >= 0x20);
  326. int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  327. lat += dlat;
  328.  
  329. shift = 0;
  330. result = 0;
  331. do {
  332. b = encoded.charAt(index++) - 63;
  333. result |= (b & 0x1f) << shift;
  334. shift += 5;
  335. } while (b >= 0x20);
  336. int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
  337. lng += dlng;
  338.  
  339. LatLng p = new LatLng((((double) lat / 1E5)),
  340. (((double) lng / 1E5)));
  341. poly.add(p);
  342. }
  343. return poly;
  344. }
  345.  
  346.  
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement