1

I want to make multiple polyline in map. i have one polyline in maps, then i want to make new polyline again. but the line is always connect with previous polyline. what should i do, this is my code:

package com.evy;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class Tambah extends FragmentActivity implements LocationListener{
    GoogleMap map;
    LocationManager lm;
    boolean isNewPoly=false;
    PolylineOptions polyline;
    ArrayList<LatLng> poly = new ArrayList<LatLng>();
    ArrayList<LatLng> points = new ArrayList<LatLng>();
    PolylineOptions polylineOptions;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tambah);

        //points=new ArrayList<LatLng>();
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.maptambah);
        map=mapFragment.getMap();


        final LocationListener ll=new LocationListener(){

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(),location.getLongitude())).title("posisi anda").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), 15.0f));
            }

            @Override
            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }

        };

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

        Button stop = (Button) findViewById(R.id.stop);
        Button start = (Button) findViewById(R.id.start);



        map.setOnMapClickListener(new OnMapClickListener(){

            @Override
            public void onMapClick(LatLng point) {
                // TODO Auto-generated method stub
                if(isNewPoly==true){
                    MarkerOptions markerOptions = new MarkerOptions().position(point).title("Position").snippet("Latitude: "+point.latitude+" , "+"Longitude: "+point.longitude).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
                    map.addMarker(markerOptions);
                    polylineOptions = new PolylineOptions().color(Color.BLUE).width(3);
                    points.add(point);
                    for(int i=0;i<points.size();i++){
                        polylineOptions.add(points.get(i));
                    }
                    map.addPolyline(polylineOptions);
                }
                for(int i=0;i<points.size();i++){
                    poly.add(points.get(i));
                }

            }
        }); 

        start.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                isNewPoly=true;
            }

        });

        stop.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                isNewPoly=false;
            }

        });
    }

    public static String encode(final List<LatLng> path){
        long lastLat = 0;
        long lastLng = 0;

        final StringBuffer result = new StringBuffer();

        for(final LatLng point:path){
            long lat = Math.round(point.latitude * 1e5);
            long lng = Math.round(point.longitude * 1e5);

            long dlat = lat - lastLat;
            long dlng = lng - lastLng;

            encoded(dlat, result);
            encoded(dlng, result);

            lastLat = lat;
            lastLng = lng;
        }
        return result.toString();
    }

    private static void encoded(long v, StringBuffer result){
        v = v < 0 ? ~(v<<1) : v<<1;
        while(v >= 0x20){
            result.append(Character.toChars((int) ((0x20 | (v & 0x1f))+63)));
            v >>= 5;
        }
        result.append(Character.toChars((int) (v + 63)));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.tambah, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch(item.getItemId()){
        case R.id.home:
            this.finish();
            startActivity(new Intent(Tambah.this, Peta.class));
            return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

if I press button start, it make new polyline, and press button stop to stop it, but it's not work. the button stop not warking.

emmanuel
  • 9,607
  • 10
  • 25
  • 38
  • Check this, http://stackoverflow.com/questions/26601425/draw-a-path-on-maps-following-the-finger-motion-path-on-the-device-screen/26602500#26602500 – Sreedhu Madhu Dec 02 '14 at 06:11
  • @sreedhumadhu thanks for your answer. I try that tutor, but not work for me, when I press draw button, I can't do anything in the map. Is there a simple way to drawing polyline with button? – evy savitri Dec 02 '14 at 12:06
  • 1
    @sreedhumadhu I solve my problem. Thanks sreedhumadhu. It's just because the arraylist – evy savitri Dec 03 '14 at 06:01

3 Answers3

0

You are currently append all points at once, instead you should use a PolyLine array. Assuming that coordinates1 and coordinates2 are the LatLng[] type of arrays that is previously appended from KML file or some other source. You should write something like below;

int size=3 //determine the size of array.
   
Polyline[] polyLine;
polyLine = new Polyline[size];

   
int i =0;
while (i<size)
{
polyLine[i] = map.addPolyline(new PolylineOptions()
                            .clickable(true)
                            .add(coordinates1[i],coordinates1[i+1]));
i++;
}
Ali Ülkü
  • 86
  • 1
  • 9
0
List<LatLng> latLngList = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            latLngList.add(i, new LatLng(arrayList.get(i).getLocation_lat(), arrayList.get(i).getLocation_lang()));
           }
 
 Polyline polyline = mMap.addPolyline(new PolylineOptions()
                .clickable(true)
                .addAll(latLngList)
                .width(2).color(Color.BLUE).geodesic(true));
Joundill
  • 6,828
  • 12
  • 36
  • 50
  • Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it works better than what the OP provided. See [answer]. – ChrisGPT was on strike Jan 06 '21 at 00:49
0
List<LatLng> latLngList = new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            latLngList.add(i, new LatLng(arrayList.get(i).getLocation_lat(), arrayList.get(i).getLocation_lang()));
            }
 Polyline polyline = googleMaps.addPolyline(new PolylineOptions()
                .clickable(true)
                .addAll(latLngList)
                .width(2).color(Color.BLUE).geodesic(true));
  • While this code may answer the question, it is better to describe what the code is doing. Answers without any explanation may get marked as low quality and deleted. – Connell.O'Donnell Jan 05 '21 at 20:38