1. Are you ready for the Galaxy S20? Here is everything we know so far!

Polylines Android for a beginner

Discussion in 'Android Development' started by Allecsxx, Dec 7, 2018.

  1. Allecsxx

    Allecsxx Lurker
    Thread Starter

    Hi ,

    I;ve god this code below ... it will add markers up to 7 on the map and will draw a polyline between them markers... the only trouble is .. how to make the polyline redraw after dragging a marker to another location? it might be something really easy... please open my eyes.... ! thank youuuu







    ArrayList<LatLng> points;





    /* Location createNewLocation(double longitude, double latitude) {
    Location location = new Location("dummyprovider");
    location.setLongitude(0.0982);
    location.setLatitude(51.3762);
    return location;
    }*/
    @override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    points= new ArrayList<>();
    }


    /**
    * Manipulates the map once available.
    * This callback is triggered when the map is ready to be used.
    * This is where we can add markers or lines, add listeners or move the camera. In this case,
    * we just add a marker near Sydney, Australia.
    * If Google Play services is not installed on the device, the user will be prompted to install
    * it inside the SupportMapFragment. This method will only be triggered once the user has
    * installed Google Play services and returned to the app.
    */
    @override
    public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    mMap.getUiSettings().setZoomControlsEnabled(true);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST);


    // TODO: Consider calling
    // ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    // public void onRequestPermissionsResult(int requestCode, String[] permissions,
    // int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
    }
    mMap.setMyLocationEnabled(true);
    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener(){
    @override
    public void onMapLongClick(LatLng point) {
    // Instantiating the class MarkerOptions to plot marker on the map
    MarkerOptions markerOptions = new MarkerOptions();
    // Setting latitude and longitude of the marker position
    markerOptions.position(point);
    // Setting titile of the infowindow of the marker
    markerOptions.title("Position");
    // Setting the content of the infowindow of the marker
    markerOptions.snippet("Latitude:" + point.latitude + "," + "Longitude:" + point.longitude);
    markerOptions.draggable(true);
    mMap.addMarker(markerOptions);
    // Instantiating the class PolylineOptions to plot polyline in the map
    PolylineOptions polylineOptions = new PolylineOptions();
    // Setting the color of the polyline
    polylineOptions.color(Color.RED);
    // Setting the width of the polyline
    polylineOptions.width(3);
    // Adding the taped point to the ArrayList
    points.add(point);
    // Setting points of polyline
    polylineOptions.addAll(points);
    // Adding the polyline to the map
    mMap.addPolyline(polylineOptions);
    // Adding the marker to the map
    if(points.size() ==7){
    points.clear();
    mMap.clear();
    }
    mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
    @override
    public void onMarkerDragStart(Marker point) {
    }
    @override
    public void onMarkerDrag(Marker point) {

    mMap.clear();

    }
    @override
    public void onMarkerDragEnd(Marker point) {
    }
    });

    }
    });


    }
     


Loading...

Share This Page

Loading...