0

Hye guys I am working on the project of vehicle tracking system when user can enters the number of vehicle the the draw a poly line on the vehicles route...

I have stored route locations with their latitude and longitude into mysql database I used asynctask method to fetch this data....

now this is my code in getRoute method

getRoute method will be call on the button click when user enters the number in EditText

public void getRoute()
{
    final String BusNo = editBusNo.getText().toString().trim();

    Log.i(TAG,"value of the Bus No is "+ BusNo);

    class GetRoute extends AsyncTask<Void,Void,String>
    {
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.i(TAG,"in pre execute");
            loading = ProgressDialog.show(MapsActivity.this, "Fetching...", "Wait...", false, false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();

            Log.i(TAG,"post execute");

            JSON_STRING = s;

            Log.i(TAG,"value of json_string "+ JSON_STRING);
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequestParam("http://192.168.1.35/projects/map.php?name=", BusNo);

            Log.i(TAG,"value of s is "+ s);
            //Log.i(T,"The value of s is "+ s);
            return s;
        }
    }

    GetRoute gr = new GetRoute();
    gr.execute();
}

Looking for this type of polyline

  • I don't know this area of the Maps Android API, but as a heads up, certain operations on the map need to be done from the main UI thread, or else you get exceptions. – Tim Biegeleisen Jul 18 '17 at 05:26

2 Answers2

2

Try this,

Polyline polyline;

PolylineOptions polylineOptions = new PolylineOptions();            
JSONArray arr = response.getJSONArray("result");
for (int i = 0; i < arr.length(); i++) 
{
    JSONObject obj = arr.getJSONObject(i);
    String latitude = obj.getString("latitude");    
    String longitude = obj.getString("longitude");  

    polylineOptions.color(Color.RED);

    polylineOptions.width(3);

    Double lat = Double.parseDouble(latitude);
    Double Longitude = Double.parseDouble(longitude);

    polylineOptions.add(new LatLng(lat, Longitude));
}
polyline=map.addPolyline(polylineOptions);

Then when you want to remove it:

polyline.remove();
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • its working but its not working when the i enters the new vehicle no then previous vehicles path/route remains on map and also new vehicles path drawn.... ??? – Vaibhav Solaskar Jul 19 '17 at 08:22
  • I want to remove when the user can enters a anothe vehicle no.... then previous polylines remove and set new polyline on resp path.... in ur updated code getting error null object if i can put it in oncreate or button click so its not nw working propery – Vaibhav Solaskar Jul 19 '17 at 09:07
  • Polyline polyline; declare this variable globally .When user added new vehical no then 1st check if(polyline!=null) { polyline.remove(); } and then add new polyline – Komal12 Jul 19 '17 at 09:47
1

To plot polyline on google map you need to have List<LatLng> which are stop points between source and destination.

Once you've that List you can plot polyline using below code

// I consider here is list of latlng from database.
List<LatLng> list = getListOfLatLng();
PolylineOptions options = new PolylineOptions().width(10).color(Color.BLUE);
for (int z = 0; z < list.size(); z++) {
    LatLng point = list.get(z);
    options.add(point);
}
map.addPolyline(options);

This will plot polylines on map object of GoogleMap.

ELITE
  • 5,815
  • 3
  • 19
  • 29