add polyline array in polyline array in google maps android -
i want make multiple polyline in map. have 1 polyline in maps, want make new polyline again. line connect previous polyline. should do, 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 menu; adds items action bar if present. menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.tambah, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify 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 press button start, make new polyline, , press button stop stop it, it's not work. button stop not warking.
Comments
Post a Comment