How to show the track route using poly line in Android











up vote
1
down vote

favorite












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




























    up vote
    1
    down vote

    favorite












    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


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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 at 19:13

























      asked Nov 21 at 18:15









      Arindam Mukherjee

      94563472




      94563472
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          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





















            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',
            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








            up vote
            0
            down vote













            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

























              up vote
              0
              down vote













              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























                up vote
                0
                down vote










                up vote
                0
                down vote









                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 at 9:44









                Andrii Omelchenko

                7,33462350




                7,33462350






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks