I've been developing a map based app. In that I'm showing bus routes with the help of Polylines. Everything works fine as you can see in this image:
But when I restart the app or revisit map activity, the polyline automatically connects the last and very first points (co-ordinates),thus making a closed figure.
So is there any way by which I can stop the map from doing this?
public class MapFragment extends Fragment {
GoogleMap gmaps;
ArrayList<LatLng>coo = ProcessLatLng.getCoordinates(); //Getting co-ordinates into Array List
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.inflate(R.layout.map_fragment, container, false);
prepareMap();
return v;
}
private void prepareMap() {
int i;
if(gmaps == null)
gmaps = ((com.google.android.gms.maps.MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if(gmaps != null){
PolylineOptions pop= new PolylineOptions();
pop.width(5).color(Color.BLUE);
for(i=0;i<coo.size();i++){
pop.add(coo.get(i));
}
gmaps.addPolyline(pop);
gmaps.addMarker(new MarkerOptions().position(coo.get(0)).title("Start"));
gmaps.addMarker(new MarkerOptions().position(coo.get(coo.size()-1)).title("End"));
gmaps.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(26.846785, 80.945591), 13));
}
}
If i'm not wrong, seems like same points are being added again, like this
The first time
(P1) ---- (P2) ---- (P3)
Second time
(P1) ---- (P2) ---- (P3) ---- (P1) ---- (P2) ---- (P3)
Try moving ProcessLatLng.getCoordinates() method to prepareMap method
Try getting polyline instance, and remove before fragment gone
Polyline mPolyline;
private void prepareMap() {
....
ArrayList<LatLng> coo = ProcessLatLng.getCoordinates();
....
mPolyline = gmaps.addPolyline(pop)
}