private FolderType createSpeed(KmlRoute route, int startIndex, int endIndex) { ObjectFactory objectFactory = new ObjectFactory(); FolderType folderType = objectFactory.createFolderType(); folderType.setName(SPEED); folderType.setVisibility(FALSE); folderType.setOpen(FALSE); folderType.getAbstractFeatureGroup().add(objectFactory.createScreenOverlay(createSpeedbar())); int segmentIndex = 0; List<String> coordinates = new ArrayList<>(); Integer previousSpeedClass = null; Double previousSpeed = null; KmlPosition previous = null; List<KmlPosition> positions = route.getPositions(); // since the speed of a position is the average speed of the previous segment for (int i = startIndex; i < endIndex; i++) { KmlPosition position = positions.get(i); Double speed = null; if (position.hasSpeed()) speed = position.getSpeed(); else if (previous != null) speed = previous.calculateSpeed(position); if (speed == null) speed = previousSpeed; if (speed == null) continue; coordinates.add(createCoordinates(position, false)); int speedClass = getSpeedClass(speed); if (previousSpeedClass != null && previousSpeedClass != speedClass) { PlacemarkType placemarkType = createSpeedSegment(++segmentIndex, previousSpeedClass, coordinates); folderType.getAbstractFeatureGroup().add(objectFactory.createPlacemark(placemarkType)); coordinates.clear(); coordinates.add(createCoordinates(position, false)); } previousSpeedClass = speedClass; previousSpeed = speed; previous = position; } return segmentIndex > 0 ? folderType : null; }