Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.39 KB | None | 0 0
  1. package com.example.finalproject;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.app.AlertDialog;
  6. import android.app.FragmentManager;
  7. import android.app.ProgressDialog;
  8. import android.arch.persistence.room.Room;
  9. import android.content.DialogInterface;
  10. import android.content.pm.PackageManager;
  11. import android.graphics.Color;
  12. import android.location.Address;
  13. import android.location.Geocoder;
  14. import android.location.Location;
  15. import android.location.LocationListener;
  16. import android.location.LocationManager;
  17. import android.os.AsyncTask;
  18. import android.provider.Telephony;
  19. import android.support.annotation.LayoutRes;
  20. import android.support.annotation.NonNull;
  21. import android.support.v4.app.ActivityCompat;
  22. import android.support.v4.app.FragmentActivity;
  23. import android.os.Bundle;
  24. import android.support.v4.app.FragmentTransaction;
  25. import android.text.Layout;
  26. import android.util.Log;
  27. import android.util.Pair;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.widget.Button;
  31. import android.widget.EditText;
  32. import android.widget.TextView;
  33. import android.widget.Toast;
  34.  
  35. import com.google.android.gms.maps.CameraUpdate;
  36. import com.google.android.gms.maps.CameraUpdateFactory;
  37. import com.google.android.gms.maps.GoogleMap;
  38. import com.google.android.gms.maps.OnMapReadyCallback;
  39. import com.google.android.gms.maps.SupportMapFragment;
  40. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  41. import com.google.android.gms.maps.model.LatLng;
  42. import com.google.android.gms.maps.model.LatLngBounds;
  43. import com.google.android.gms.maps.model.MarkerOptions;
  44. import com.google.android.gms.maps.model.Polyline;
  45. import com.google.android.gms.maps.model.PolylineOptions;
  46. import com.google.android.gms.plus.model.people.Person;
  47.  
  48. import java.io.BufferedReader;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.InputStreamReader;
  52. import java.net.HttpURLConnection;
  53. import java.net.MalformedURLException;
  54. import java.net.URL;
  55. import java.util.ArrayList;
  56. import java.util.List;
  57.  
  58. import org.json.JSONArray;
  59. import org.json.JSONException;
  60. import org.json.JSONObject;
  61.  
  62. public class InitialMapActivity extends FragmentActivity implements OnMapReadyCallback {
  63.  
  64.     private LatLng myLocation = null; //currentLocation
  65.     private GPSTracker gpsTracker;
  66.     private GoogleMap mMap;
  67.     private AppDatabase db;
  68.     private Button btnOptimise;
  69.     private LatLng startLocation;
  70.     private LatLng endLocation;
  71.  
  72.     private List<String> addresses = new ArrayList<>();
  73.     private List<LatLng> latLngs = new ArrayList<>();
  74.     private List<String> optimisedAddresses = new ArrayList<>();
  75.     private List<LatLng> optimisedLatLngs = new ArrayList<>();
  76.     private List<MarkerOptions> markers = new ArrayList<>();
  77.     private List<String> jobID = new ArrayList<>();
  78.     private List<String> optimisedJobID = new ArrayList<>();
  79.  
  80.     String txtJson;
  81.     ProgressDialog pd;
  82.  
  83.     @Override
  84.     public void onBackPressed(){
  85.         android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
  86.         if (fm.getBackStackEntryCount() > 0) {
  87.             Log.i("MainActivity", "popping backstack");
  88.             fm.popBackStackImmediate();
  89.         } else {
  90.             Log.i("MainActivity", "nothing on backstack, calling super");
  91.             super.onBackPressed();
  92.         }
  93.     }
  94.  
  95.     //create a paired list first is adresss second is latlongs
  96.     private Pair<List<String>, List<LatLng> > pair = Pair.create(addresses, latLngs);
  97.  
  98.     @Override
  99.     protected void onCreate(Bundle savedInstanceState) {
  100.         super.onCreate(savedInstanceState);
  101.         setContentView(R.layout.activity_initial_map);
  102.  
  103.         ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
  104.  
  105.         LocationListener mLocationListener = new LocationListener() {
  106.             @Override
  107.             public void onLocationChanged(Location location) {
  108.                 myLocation = new LatLng(location.getLatitude(), location.getLongitude());
  109.                 SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  110.                 supportMapFragment.getMapAsync(InitialMapActivity.this);
  111.             }
  112.  
  113.             @Override
  114.             public void onStatusChanged(String provider, int status, Bundle extras) {
  115.  
  116.             }
  117.  
  118.             @Override
  119.             public void onProviderEnabled(String provider) {
  120.  
  121.             }
  122.  
  123.             @Override
  124.             public void onProviderDisabled(String provider) {
  125.  
  126.             }
  127.         };
  128.  
  129.         LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  130.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  131.             mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,
  132.                     10, mLocationListener);
  133.         }
  134.  
  135.         btnOptimise = findViewById(R.id.btn_Optimise);
  136.         btnOptimise.setOnClickListener(new View.OnClickListener() {
  137.             public void onClick(View v) {
  138.  
  139.  
  140.                 addresses = pair.first;
  141.                 latLngs = pair.second;
  142.  
  143.                 addresses.add(0, "Start");
  144.                 addresses.add("end");
  145.                 latLngs.add(0, startLocation);
  146.                 latLngs.add(endLocation);
  147.  
  148.                 String latlngString = latLngs.toString();
  149.                 latlngString = latlngString.replaceAll("\\), lat/lng: \\(", ":");
  150.                 latlngString = latlngString.replaceAll("\\[lat/lng: \\(", "");
  151.                 latlngString = latlngString.replaceAll("\\)]", "");
  152.  
  153.  
  154.                 Log.d("TESTING!!", "onPostExecute: " + addresses);
  155.                 Log.d("TESTING!!", "onPostExecute: " + latlngString);
  156.                 //TO DO:
  157.                 //now we need to get optimal order and route using the tom tom api
  158.                 //we have a list of addressses and corisponding latlngs
  159.  
  160.                 //for live use this should be obfuscated
  161.                 String key = "5jamNFMokhWdAqpWIxGjNh388PHjJP69";
  162.  
  163.                 String url = "https://api.tomtom.com/routing/1/calculateRoute/" + latlngString +
  164.                         "/json?computeBestOrder=true&routeRepresentation=polyline&" +
  165.                         "computeTravelTimeFor=none&routeType=fastest&traffic=false&" +
  166.                         "avoid=unpavedRoads&travelMode=car&key=" + key;
  167.  
  168.                 new JsonTask().execute(url);
  169.             }
  170.         });
  171.     }
  172.  
  173.     private void getOptimisedRoute()
  174.     {
  175.         JSONObject obj = null;
  176.         try {
  177.             obj = new JSONObject(txtJson);
  178.             JSONArray optimizedWaypoints = obj.getJSONArray("optimizedWaypoints");
  179.             Log.d("TESTING!!", "optimizedWaypoints: " + optimizedWaypoints);
  180.  
  181.             for(int i=0;i<optimizedWaypoints.length(); i++)
  182.             {
  183.                 String temp = optimizedWaypoints.getString(i);
  184.                 JSONObject obj2 = new JSONObject(temp);
  185.                 int optimised = Integer.parseInt(obj2.getString("optimizedIndex")) + 1;
  186.                 optimisedAddresses.add(addresses.get(optimised));
  187.                 optimisedLatLngs.add(latLngs.get(optimised));
  188.                 optimisedJobID.add(jobID.get(optimised - 1));
  189.                 db.myDao().updateJobLatlng(latLngs.get(optimised - 1).toString(), jobID.get(optimised - 1));
  190.                 db.myDao().updateJobOrder(optimised  - 1, jobID.get(optimised  - 1));
  191.             }
  192.         } catch (JSONException e) {
  193.             e.printStackTrace();
  194.         }
  195.         Log.d("TESTING!!", "optimizedWaypoints: " + optimisedAddresses);
  196.     }
  197.  
  198.     private void drawPollyLines()
  199.     {
  200.         for (ArrayList<String> line : pollyLines) {
  201.             PolylineOptions rectOptions = new PolylineOptions();
  202.             for (String subline : line)
  203.             {
  204.                 String[] latlong =  subline.split(",");
  205.                 double latitude = Double.parseDouble(latlong[0]);
  206.                 double longitude = Double.parseDouble(latlong[1]);
  207.                 rectOptions.add(new LatLng(latitude,longitude));
  208.             }
  209.  
  210.             rectOptions.color(Color.argb(100,0,0,255));
  211.             rectOptions.width(25);
  212.             Polyline polyline = mMap.addPolyline(rectOptions);
  213.         }
  214.     }
  215.  
  216.     private ArrayList<ArrayList<String>> pollyLines = new ArrayList<>();
  217.     private void getPollyLines() {
  218.         JSONObject obj = null;
  219.         try {
  220.             obj = new JSONObject(txtJson);
  221.             JSONArray routes = obj.optJSONArray("routes");
  222.             Log.d("TESTING!!", "routes: " + routes);
  223.             Log.d("TESTING!!", "size of routes: " + routes.length());
  224.  
  225.             for(int i=0;i<routes.length(); i++)
  226.                 {
  227.                   String tempLegs = routes.getString(i);
  228.                   JSONObject obj2 = new JSONObject(tempLegs);
  229.                   JSONArray legs = obj2.getJSONArray("legs");
  230.                     for (int y=0;y<legs.length();y++)
  231.                     {
  232.                         String tempPoints = legs.getString(y);
  233.                         JSONObject obj3 = new JSONObject(tempPoints);
  234.                         JSONArray points = obj3.getJSONArray("points");
  235.                         ArrayList<String> legArray = new ArrayList<>();
  236.                         for (int u = 0;u<points.length();u++)
  237.                         {
  238.                             String temp = points.getString(u);
  239.  
  240.                             temp = temp.replaceAll("\\{\"latitude\":", "");
  241.                             temp = temp.replaceAll("\"longitude\":", "");
  242.                             temp = temp.replaceAll("\\}", "");
  243.  
  244.                             legArray.add(temp);
  245.                         }
  246.                         pollyLines.add(legArray);
  247.                     }
  248.                 }
  249.         } catch (JSONException e) {
  250.             e.printStackTrace();
  251.         }
  252.     }
  253.  
  254.     private class JsonTask extends AsyncTask<String, String, String> {
  255.  
  256.         protected void onPreExecute() {
  257.             super.onPreExecute();
  258.  
  259.             pd = new ProgressDialog(InitialMapActivity.this);
  260.             pd.setMessage("Please wait");
  261.             pd.setCancelable(false);
  262.             pd.show();
  263.         }
  264.  
  265.         protected String doInBackground(String... params) {
  266.  
  267.             HttpURLConnection connection = null;
  268.             BufferedReader reader = null;
  269.  
  270.             try {
  271.                 URL url = new URL(params[0]);
  272.                 connection = (HttpURLConnection) url.openConnection();
  273.                 connection.connect();
  274.  
  275.                 InputStream stream = connection.getInputStream();
  276.  
  277.                 reader = new BufferedReader(new InputStreamReader(stream));
  278.  
  279.                 StringBuffer buffer = new StringBuffer();
  280.                 String line = "";
  281.  
  282.                 while ((line = reader.readLine()) != null) {
  283.                     buffer.append(line+"\n");
  284.                     Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)
  285.                 }
  286.                 return buffer.toString();
  287.  
  288.             } catch (MalformedURLException e) {
  289.                 e.printStackTrace();
  290.             } catch (IOException e) {
  291.                 e.printStackTrace();
  292.             } finally {
  293.                 if (connection != null) {
  294.                     connection.disconnect();
  295.                 }
  296.                 try {
  297.                     if (reader != null) {
  298.                         reader.close();
  299.                     }
  300.                 } catch (IOException e) {
  301.                     e.printStackTrace();
  302.                 }
  303.             }
  304.             return null;
  305.         }
  306.  
  307.         @Override
  308.         protected void onPostExecute(String result) {
  309.             super.onPostExecute(result);
  310.             if (pd.isShowing()){
  311.                 pd.dismiss();
  312.             }
  313.             txtJson = result;
  314.             getOptimisedRoute();
  315.             getPollyLines();
  316.             drawPollyLines();
  317.  
  318.             //going about this wrong, need to start new activity with order then start new map i think0
  319.             android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
  320.  
  321.             FragmentTransaction transaction = manager.beginTransaction();
  322.             transaction.add(R.id.address_list_fragment, AddressFragment.newInstance(optimisedAddresses)).addToBackStack("tag");
  323.             transaction.hide(manager.findFragmentById(R.id.map));
  324.             transaction.commit();
  325.  
  326.  
  327.         }
  328.     }
  329.  
  330.     @Override
  331.     protected void onResume() {
  332.         super.onResume();
  333.  
  334.         if (checkLocationPermission()) {
  335.             gpsTracker = new GPSTracker(this);
  336.             if (gpsTracker.canGetLocation) {
  337.                 myLocation = new LatLng(gpsTracker.getLatitude(), gpsTracker.getLongitude());
  338.             } else {
  339.                 Toast.makeText(this, "please accept permission !!!!", Toast.LENGTH_SHORT).show();
  340.                 finish();
  341.             }
  342.         }
  343.     }
  344.  
  345.     public boolean checkLocationPermission() {
  346.         String permission = "android.permission.ACCESS_FINE_LOCATION";
  347.         int res = this.checkCallingOrSelfPermission(permission);
  348.         return (res == PackageManager.PERMISSION_GRANTED);
  349.     }
  350.  
  351.     @SuppressLint("MissingPermission")
  352.     @Override
  353.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  354.  
  355.         // If request is cancelled, the result arrays are empty.
  356.         if (grantResults.length > 0
  357.                 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  358.             //Permission Granted
  359.             gpsTracker = new GPSTracker(this);
  360.             if (gpsTracker.canGetLocation) {
  361.                 mMap.setMyLocationEnabled(true);
  362.                 myLocation = new LatLng(gpsTracker.getLatitude(), gpsTracker.getLongitude());
  363.             }
  364.         }
  365.     }
  366.  
  367.     @Override
  368.     public void onMapReady(GoogleMap googleMap) {
  369.         mMap = googleMap;
  370.         //get locations on seperate thread
  371.         new AsyncShowLocations().execute(pair);
  372.     }
  373.  
  374.  
  375.     private void getEndLocation()
  376.     {
  377.         // get prompts.xml view
  378.         LayoutInflater li = LayoutInflater.from(InitialMapActivity.this);
  379.         View promptsView = li.inflate(R.layout.prompt_getstart, null);
  380.  
  381.         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  382.                 InitialMapActivity.this);
  383.  
  384.         // set prompts.xml to alertdialog builder
  385.         alertDialogBuilder.setView(promptsView);
  386.  
  387.         final EditText userInput = promptsView
  388.                 .findViewById(R.id.editTextDialogUserInput);
  389.  
  390.         TextView alertTxt = promptsView.findViewById(R.id.alertTxtView);
  391.         alertTxt.setText("Use custom end location?");
  392.  
  393.         // set dialog message
  394.         alertDialogBuilder
  395.                 .setCancelable(false)
  396.                 .setPositiveButton("Yes",
  397.                         new DialogInterface.OnClickListener() {
  398.                             public void onClick(DialogInterface dialog,int id) {
  399.                                 // get user input and set it to result
  400.                                 // edit text
  401.                                 LatLng newlatlng = getLatLngFromAddress(userInput.getText().toString());
  402.                                 if (newlatlng != null) {
  403.                                     endLocation = newlatlng;
  404.                                     MarkerOptions marker = new MarkerOptions()
  405.                                             .position(endLocation)
  406.                                             .title("End")
  407.                                             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
  408.                                     markers.add(marker);
  409.                                     mMap.addMarker(marker);
  410.                                     zoomMap();
  411.                                 } else
  412.                                 {
  413.                                     TextView alertTxtView = findViewById(R.id.alertTxtView);
  414.                                     alertTxtView.setText("Could not find address");
  415.                                 }
  416.                             }
  417.                         })
  418.                 .setNegativeButton("Use My Location",
  419.                         new DialogInterface.OnClickListener() {
  420.                             public void onClick(DialogInterface dialog,int id) {
  421.                                 dialog.cancel();
  422.                                 endLocation = myLocation;
  423.                                 MarkerOptions marker = new MarkerOptions()
  424.                                         .position(endLocation)
  425.                                         .title("End")
  426.                                         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
  427.  
  428.                                 markers.add(marker);
  429.                                 mMap.addMarker(marker);
  430.                                 zoomMap();
  431.                             }
  432.                         });
  433.  
  434.         // create alert dialog
  435.         AlertDialog alertDialog = alertDialogBuilder.create();
  436.         // show it
  437.         alertDialog.show();
  438.     }
  439.  
  440.     private void getStartLocation()
  441.     {
  442.         // get prompts.xml view
  443.         LayoutInflater li = LayoutInflater.from(InitialMapActivity.this);
  444.         View promptsView = li.inflate(R.layout.prompt_getstart, null);
  445.  
  446.         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
  447.                 InitialMapActivity.this);
  448.  
  449.         // set prompts.xml to alertdialog builder
  450.         alertDialogBuilder.setView(promptsView);
  451.  
  452.         final EditText userInput = promptsView
  453.                 .findViewById(R.id.editTextDialogUserInput);
  454.  
  455.         TextView alertTxt = promptsView.findViewById(R.id.alertTxtView);
  456.         alertTxt.setText("Use custom start location?");
  457.  
  458.         // set dialog message
  459.         alertDialogBuilder
  460.                 .setCancelable(false)
  461.                 .setPositiveButton("Yes",
  462.                         new DialogInterface.OnClickListener() {
  463.                             public void onClick(DialogInterface dialog,int id) {
  464.                                 // get user input and set it to result
  465.                                 // edit text
  466.                                 LatLng newlatlng = getLatLngFromAddress(userInput.getText().toString());
  467.                                 if (newlatlng != null) {
  468.                                     startLocation = newlatlng;
  469.                                     MarkerOptions marker = new MarkerOptions()
  470.                                             .position(startLocation)
  471.                                             .title("Start")
  472.                                             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
  473.                                     markers.add(marker);
  474.                                     mMap.addMarker(marker);
  475.                                     zoomMap();
  476.                                     getEndLocation();
  477.                                 } else
  478.                                 {
  479.                                     TextView alertTxtView = findViewById(R.id.alertTxtView);
  480.                                     alertTxtView.setText("Could not find address");
  481.                                 }
  482.                             }
  483.                         })
  484.                 .setNegativeButton("Use My Location",
  485.                         new DialogInterface.OnClickListener() {
  486.                             public void onClick(DialogInterface dialog,int id) {
  487.                                 dialog.cancel();
  488.                                 startLocation = myLocation;
  489.                                 MarkerOptions marker = new MarkerOptions()
  490.                                         .position(startLocation)
  491.                                         .title("Start")
  492.                                         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
  493.  
  494.                                 markers.add(marker);
  495.                                 mMap.addMarker(marker);
  496.                                 zoomMap();
  497.                                 getEndLocation();
  498.                             }
  499.                         });
  500.  
  501.         // create alert dialog
  502.         AlertDialog alertDialog = alertDialogBuilder.create();
  503.         // show it
  504.         alertDialog.show();
  505.     }
  506.  
  507.     private class AsyncShowLocations extends AsyncTask<Pair<List<String>, List<LatLng> >, Void, Pair<List<String>, List<LatLng> >>
  508.     {
  509.         List<String> addresses;
  510.         List<LatLng> latLngs;
  511.  
  512.         protected void onPreExecute() {
  513.             super.onPreExecute();
  514.  
  515.             pd = new ProgressDialog(InitialMapActivity.this);
  516.             pd.setMessage("Please wait");
  517.             pd.setCancelable(false);
  518.             pd.show();
  519.         }
  520.  
  521.         @Override
  522.         protected Pair doInBackground(Pair<List<String>, List<LatLng> >... pair) {
  523.  
  524.             addresses = getAddresses();
  525.             latLngs = getLocationFromAddress(addresses);
  526.             while (latLngs.contains(null)) {
  527.                 latLngs.clear();
  528.                 latLngs = getLocationFromAddress(addresses);
  529.             }
  530.             return Pair.create(addresses, latLngs);
  531.         }
  532.  
  533.         @Override
  534.         protected void onPostExecute(Pair<List<String>, List<LatLng> > result) {
  535.             super.onPostExecute(result);
  536.             showLocations(addresses, latLngs);
  537.             pair.first.addAll(result.first);
  538.             pair.second.addAll(result.second);
  539.             btnOptimise.setVisibility(View.VISIBLE);
  540.  
  541.             if (pd.isShowing()){
  542.                 pd.dismiss();
  543.             }
  544.  
  545.             //get prompt for start location and add to map
  546.             getStartLocation();
  547.         }
  548.     }
  549.  
  550.     private void showLocations(List<String> addresses, List<LatLng> latLngs)
  551.     {
  552.         for(int i = 0; i < latLngs.size(); i++)
  553.         {
  554.             String address = addresses.get(i);
  555.             LatLng latLng = latLngs.get(i);
  556.  
  557.             MarkerOptions marker = new MarkerOptions().position(latLng).title(address);
  558.  
  559.             markers.add(marker);
  560.             //Put marker on map on that LatLng
  561.             mMap.addMarker(marker);
  562.         }
  563.  
  564.         zoomMap();
  565.     }
  566.  
  567.     private void zoomMap()
  568.     {
  569.         LatLngBounds.Builder builder = new LatLngBounds.Builder();
  570.         for (MarkerOptions marker : markers) {
  571.             builder.include(marker.getPosition());
  572.         }
  573.  
  574.         //Animate and Zoom on that map location
  575.         LatLngBounds bounds = builder.build();
  576.         int padding = 100; // offset from edges of the map in pixels
  577.         CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
  578.         mMap.animateCamera(cu);
  579.     }
  580.  
  581.     private List<String> getAddresses(){
  582.  
  583.         List<String> Addresses = new ArrayList<>();
  584.  
  585.         db = Room.databaseBuilder(this,
  586.                 AppDatabase.class, "sessionDatabase").allowMainThreadQueries().build();
  587.  
  588.         Job[] jobs = db.myDao().getAllJobs();
  589.         for (Job job : jobs) {
  590.             Addresses.add(job.getAddress() + " " + job.getPostcode());
  591.             jobID.add(job.getJobID());
  592.         }
  593.         return Addresses;
  594.     }
  595.  
  596.     private List<LatLng> getLocationFromAddress(List<String> addresses)
  597.     {
  598.         List<LatLng> latLngs = new ArrayList<>();
  599.         for (String address : addresses)
  600.         {
  601.             LatLng latLng;
  602.             latLng = getLatLngFromAddress(address);
  603.             latLngs.add(latLng);
  604.         }
  605.         return latLngs;
  606.     }
  607.  
  608.     private LatLng getLatLngFromAddress(String address)
  609.     {
  610.         Geocoder coder = new Geocoder(this);
  611.         List<Address> possibleaddresses;
  612.         LatLng latLng = null;
  613.         try {
  614.             //Get latLng from String
  615.             possibleaddresses = coder.getFromLocationName(address,5);
  616.  
  617.             //check for null
  618.             if (possibleaddresses == null) {
  619.                 return null;
  620.             }
  621.  
  622.             //Lets take first possibility from the all possibilities.
  623.             Address location = possibleaddresses.get(0);
  624.             latLng = new LatLng(location.getLatitude(), location.getLongitude());
  625.  
  626.         } catch (IOException e)
  627.         {
  628.             e.printStackTrace();
  629.         }
  630.         return latLng;
  631.     }
  632. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement