How to show the track route using poly line in Android












1















I am developing a bus tracking application where I am getting the location using service from server. Now, with that I want to show the bus movement and draw a proper polyline. I achieved a part of this but facing two main issues:




  1. Every time bus marker is showing, it is not getting removed. So, the older footprint of the bus is still present. Although I reached destination, I am seeing many bus icons.


  2. I am able to draw the polyline by joining the latitude and longitude but it is showing sometimes a straight line.



I have attached two screenshots for that.
enter image description here



enter image description here
The code which I used is here:



private void setmMap() {
progressDialog.show();

if (broadcastReceiver == null)
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Testing", "inside the setmap");
// show progress dialog
try {
double latitude = intent.getDoubleExtra("lat", 22.560214);
double longitude = intent.getDoubleExtra("longi", 22.560214);
Log.d("SetMap", intent.getExtras().getString("time"));
LatLng startLocation = new LatLng(latitude, longitude);
m.setPosition(startLocation);
points.add(startLocation);

PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
}
line = mMap.addPolyline(options); //add Polyline
mMap.moveCamera(CameraUpdateFactory.newLatLng(startLocation));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startLocation, 15));
progressDialog.cancel();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String strLoc = "Latitude:: "+latitude+" ::Longitude:: "+longitude+" ::Address:: "+address+" "+city+" "+
state;
Toast.makeText(getApplicationContext(),strLoc, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
progressDialog.cancel();
}
}
};
registerReceiver(broadcastReceiver, new IntentFilter("carLocationService"));
}


Thanks,
Arindam.










share|improve this question





























    1















    I am developing a bus tracking application where I am getting the location using service from server. Now, with that I want to show the bus movement and draw a proper polyline. I achieved a part of this but facing two main issues:




    1. Every time bus marker is showing, it is not getting removed. So, the older footprint of the bus is still present. Although I reached destination, I am seeing many bus icons.


    2. I am able to draw the polyline by joining the latitude and longitude but it is showing sometimes a straight line.



    I have attached two screenshots for that.
    enter image description here



    enter image description here
    The code which I used is here:



    private void setmMap() {
    progressDialog.show();

    if (broadcastReceiver == null)
    broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    Log.d("Testing", "inside the setmap");
    // show progress dialog
    try {
    double latitude = intent.getDoubleExtra("lat", 22.560214);
    double longitude = intent.getDoubleExtra("longi", 22.560214);
    Log.d("SetMap", intent.getExtras().getString("time"));
    LatLng startLocation = new LatLng(latitude, longitude);
    m.setPosition(startLocation);
    points.add(startLocation);

    PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int i = 0; i < points.size(); i++) {
    LatLng point = points.get(i);
    options.add(point);
    }
    line = mMap.addPolyline(options); //add Polyline
    mMap.moveCamera(CameraUpdateFactory.newLatLng(startLocation));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startLocation, 15));
    progressDialog.cancel();
    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(context, Locale.getDefault());
    addresses = geocoder.getFromLocation(latitude, longitude, 1);

    String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
    String city = addresses.get(0).getLocality();
    String state = addresses.get(0).getAdminArea();
    String country = addresses.get(0).getCountryName();
    String postalCode = addresses.get(0).getPostalCode();
    String strLoc = "Latitude:: "+latitude+" ::Longitude:: "+longitude+" ::Address:: "+address+" "+city+" "+
    state;
    Toast.makeText(getApplicationContext(),strLoc, Toast.LENGTH_LONG).show();
    } catch (Exception e) {
    e.printStackTrace();
    progressDialog.cancel();
    }
    }
    };
    registerReceiver(broadcastReceiver, new IntentFilter("carLocationService"));
    }


    Thanks,
    Arindam.










    share|improve this question



























      1












      1








      1








      I am developing a bus tracking application where I am getting the location using service from server. Now, with that I want to show the bus movement and draw a proper polyline. I achieved a part of this but facing two main issues:




      1. Every time bus marker is showing, it is not getting removed. So, the older footprint of the bus is still present. Although I reached destination, I am seeing many bus icons.


      2. I am able to draw the polyline by joining the latitude and longitude but it is showing sometimes a straight line.



      I have attached two screenshots for that.
      enter image description here



      enter image description here
      The code which I used is here:



      private void setmMap() {
      progressDialog.show();

      if (broadcastReceiver == null)
      broadcastReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
      Log.d("Testing", "inside the setmap");
      // show progress dialog
      try {
      double latitude = intent.getDoubleExtra("lat", 22.560214);
      double longitude = intent.getDoubleExtra("longi", 22.560214);
      Log.d("SetMap", intent.getExtras().getString("time"));
      LatLng startLocation = new LatLng(latitude, longitude);
      m.setPosition(startLocation);
      points.add(startLocation);

      PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
      for (int i = 0; i < points.size(); i++) {
      LatLng point = points.get(i);
      options.add(point);
      }
      line = mMap.addPolyline(options); //add Polyline
      mMap.moveCamera(CameraUpdateFactory.newLatLng(startLocation));
      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startLocation, 15));
      progressDialog.cancel();
      Geocoder geocoder;
      List<Address> addresses;
      geocoder = new Geocoder(context, Locale.getDefault());
      addresses = geocoder.getFromLocation(latitude, longitude, 1);

      String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
      String city = addresses.get(0).getLocality();
      String state = addresses.get(0).getAdminArea();
      String country = addresses.get(0).getCountryName();
      String postalCode = addresses.get(0).getPostalCode();
      String strLoc = "Latitude:: "+latitude+" ::Longitude:: "+longitude+" ::Address:: "+address+" "+city+" "+
      state;
      Toast.makeText(getApplicationContext(),strLoc, Toast.LENGTH_LONG).show();
      } catch (Exception e) {
      e.printStackTrace();
      progressDialog.cancel();
      }
      }
      };
      registerReceiver(broadcastReceiver, new IntentFilter("carLocationService"));
      }


      Thanks,
      Arindam.










      share|improve this question
















      I am developing a bus tracking application where I am getting the location using service from server. Now, with that I want to show the bus movement and draw a proper polyline. I achieved a part of this but facing two main issues:




      1. Every time bus marker is showing, it is not getting removed. So, the older footprint of the bus is still present. Although I reached destination, I am seeing many bus icons.


      2. I am able to draw the polyline by joining the latitude and longitude but it is showing sometimes a straight line.



      I have attached two screenshots for that.
      enter image description here



      enter image description here
      The code which I used is here:



      private void setmMap() {
      progressDialog.show();

      if (broadcastReceiver == null)
      broadcastReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
      Log.d("Testing", "inside the setmap");
      // show progress dialog
      try {
      double latitude = intent.getDoubleExtra("lat", 22.560214);
      double longitude = intent.getDoubleExtra("longi", 22.560214);
      Log.d("SetMap", intent.getExtras().getString("time"));
      LatLng startLocation = new LatLng(latitude, longitude);
      m.setPosition(startLocation);
      points.add(startLocation);

      PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
      for (int i = 0; i < points.size(); i++) {
      LatLng point = points.get(i);
      options.add(point);
      }
      line = mMap.addPolyline(options); //add Polyline
      mMap.moveCamera(CameraUpdateFactory.newLatLng(startLocation));
      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startLocation, 15));
      progressDialog.cancel();
      Geocoder geocoder;
      List<Address> addresses;
      geocoder = new Geocoder(context, Locale.getDefault());
      addresses = geocoder.getFromLocation(latitude, longitude, 1);

      String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
      String city = addresses.get(0).getLocality();
      String state = addresses.get(0).getAdminArea();
      String country = addresses.get(0).getCountryName();
      String postalCode = addresses.get(0).getPostalCode();
      String strLoc = "Latitude:: "+latitude+" ::Longitude:: "+longitude+" ::Address:: "+address+" "+city+" "+
      state;
      Toast.makeText(getApplicationContext(),strLoc, Toast.LENGTH_LONG).show();
      } catch (Exception e) {
      e.printStackTrace();
      progressDialog.cancel();
      }
      }
      };
      registerReceiver(broadcastReceiver, new IntentFilter("carLocationService"));
      }


      Thanks,
      Arindam.







      android google-maps google-polyline






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 19:13







      Arindam Mukherjee

















      asked Nov 21 '18 at 18:15









      Arindam MukherjeeArindam Mukherjee

      97063472




      97063472
























          1 Answer
          1






          active

          oldest

          votes


















          0














          1) you din't show part of code where marker m added, possible that code runs several times;



          2) seems bus location sensor's polling period is quite large and does not allow tracking bus turns (bus can make several turns between it's known locations). So you need to interpolate path between known bus locations for example with Google Directions API.






          share|improve this answer
























          • I have modified some part. Now I can see a single map icon. But my requirement is to show a polyline between the start and end point and animate the marker. The location of the marker will get updated each 30 secs. I am getting the latitude and longitude each 30 secs.

            – Arindam Mukherjee
            Dec 4 '18 at 18:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418264%2fhow-to-show-the-track-route-using-poly-line-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          1) you din't show part of code where marker m added, possible that code runs several times;



          2) seems bus location sensor's polling period is quite large and does not allow tracking bus turns (bus can make several turns between it's known locations). So you need to interpolate path between known bus locations for example with Google Directions API.






          share|improve this answer
























          • I have modified some part. Now I can see a single map icon. But my requirement is to show a polyline between the start and end point and animate the marker. The location of the marker will get updated each 30 secs. I am getting the latitude and longitude each 30 secs.

            – Arindam Mukherjee
            Dec 4 '18 at 18:18
















          0














          1) you din't show part of code where marker m added, possible that code runs several times;



          2) seems bus location sensor's polling period is quite large and does not allow tracking bus turns (bus can make several turns between it's known locations). So you need to interpolate path between known bus locations for example with Google Directions API.






          share|improve this answer
























          • I have modified some part. Now I can see a single map icon. But my requirement is to show a polyline between the start and end point and animate the marker. The location of the marker will get updated each 30 secs. I am getting the latitude and longitude each 30 secs.

            – Arindam Mukherjee
            Dec 4 '18 at 18:18














          0












          0








          0







          1) you din't show part of code where marker m added, possible that code runs several times;



          2) seems bus location sensor's polling period is quite large and does not allow tracking bus turns (bus can make several turns between it's known locations). So you need to interpolate path between known bus locations for example with Google Directions API.






          share|improve this answer













          1) you din't show part of code where marker m added, possible that code runs several times;



          2) seems bus location sensor's polling period is quite large and does not allow tracking bus turns (bus can make several turns between it's known locations). So you need to interpolate path between known bus locations for example with Google Directions API.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 9:44









          Andrii OmelchenkoAndrii Omelchenko

          7,78262452




          7,78262452













          • I have modified some part. Now I can see a single map icon. But my requirement is to show a polyline between the start and end point and animate the marker. The location of the marker will get updated each 30 secs. I am getting the latitude and longitude each 30 secs.

            – Arindam Mukherjee
            Dec 4 '18 at 18:18



















          • I have modified some part. Now I can see a single map icon. But my requirement is to show a polyline between the start and end point and animate the marker. The location of the marker will get updated each 30 secs. I am getting the latitude and longitude each 30 secs.

            – Arindam Mukherjee
            Dec 4 '18 at 18:18

















          I have modified some part. Now I can see a single map icon. But my requirement is to show a polyline between the start and end point and animate the marker. The location of the marker will get updated each 30 secs. I am getting the latitude and longitude each 30 secs.

          – Arindam Mukherjee
          Dec 4 '18 at 18:18





          I have modified some part. Now I can see a single map icon. But my requirement is to show a polyline between the start and end point and animate the marker. The location of the marker will get updated each 30 secs. I am getting the latitude and longitude each 30 secs.

          – Arindam Mukherjee
          Dec 4 '18 at 18:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418264%2fhow-to-show-the-track-route-using-poly-line-in-android%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin