2016-07-23 11 views
1

In meiner App zeichne ich eine Polylinie für jeden Standortwechsel, während ich herumwandere. Dies kann für eine 8-stündige Backpacking-Wanderung sein, so dass ich potenziell Zehntausende von Punkten zum Plotten haben kann.PolyLines auf Google Maps Android für jede Standortänderung, neue Polylinie oder setPoints?

In meinen Tests habe ich festgestellt, dass, wenn ich ziemlich nah herangezoomt (sagen 17 oder 18), es funktioniert auch nach dem Plotten von Tausenden von Zeilen, aber wie ich verkleinern und die Karte muss ALL zu rendern Von diesen Zeilen wird es träge, da mein Telefon versucht, alles zu verarbeiten. Die andere Option, die ich kenne, um Polylinien zu zeichnen, besteht darin, eine Sammlung (ArrayList) aller meiner Punkte (LatLng) zu erstellen und diese an die setPoints() -Methode der PolyLine weiterzuleiten, die eine einzelne Linie zeichnet. Das funktioniert natürlich gut, wenn man den Treck nach der Tat durchgeht, aber ich könnte mir vorstellen, dass die Interna von setPoints() für jeden neuen Punkt, der hinzugefügt wird, um die eine Zeile zu zeichnen, den gesamten Satz durchlaufen muss, was letztendlich wahrscheinlich schlechter ist es muss das tun, ob die älteren Punkte sichtbar sind (innerhalb des Ansichtsfensters) oder nicht.

Gibt es etwas dazwischen? Eine polyline.addPoint() -Methode wäre perfekt, aber ich kann so etwas nicht finden ... Im Moment habe ich meine Polylinien-Sichtbarkeit umschaltbar gemacht, damit ich sie ausblenden kann, wenn ich weit zoome aus und muss die Karte verschieben, wie ich sagte, wenn ich eng herangezoomt bin, ist das kein Problem, weil es nur eine ziemlich kleine Teilmenge rendern muss.

alle Ideen/Vorschläge werden sehr geschätzt.

TIA

Antwort

2

Dies ist, was ich als eine Lösung kam. Es ist vielleicht nicht das eleganteste, aber ich bin nur über 40 Meilen in meinem Auto herumgefahren und als ich herausgezoomt habe, hatte ich keinerlei Verzögerung. Ich habe auch das Gefühl, dass ich nur die eine große Linie alle 100 Punkte erzeuge (ungefähr alle 100 Sekunden, da mein Standort-Listener nur jede Sekunde abfeuert), ich spare die Verarbeitung, dass ich mein latliges Array für jede Standortänderung loopen muss. Jetzt muss ich das nur auf einer echten Wanderung testen :)

// create and add new poly line to map from previos location to new location 
      PolylineOptions lineOptions = new PolylineOptions() 
        .add(new LatLng(previousLocation.getLatitude(), previousLocation.getLongitude())) 
        .add(new LatLng(location.getLatitude(), location.getLongitude())) 
        .color(Color.GREEN) 
        .width(5); 
      // add the polyline to the map 
      Polyline polyline = map.addPolyline(lineOptions); 
      // set the zindex so that the poly line stays on top of my tile overlays 
      polyline.setZIndex(1000); 
      // add the poly line to the array so they can all be removed if necessary 
      polylines.add(polyline); 
      // add the latlng from this point to the array 
      allLatLngs.add(currentLocationLatLng); 

      // check if the positions added is a multiple of 100, if so, redraw all of the polylines as one line (this helps with rendering the map when there are thousands of points) 
      if(PositionsAdded % 100 == 0) { 
       // first remove all of the existing polylines 
       for(Polyline pline : polylines) { 
        pline.remove(); 
       } 
       // create one new large polyline 
       Polyline routeSoFar = map.addPolyline(new PolylineOptions().color(Color.GREEN).width(5)); 
       // draw the polyline for the route so far 
       routeSoFar.setPoints(allLatLngs); 
       // set the zindex so that the poly line stays on top of my tile overlays 
       routeSoFar.setZIndex(1000); 
       // clear the polylines array 
       polylines.clear(); 
       // add the new poly line as the first element in the polylines array 
       polylines.add(routeSoFar); 
      }